回答編集履歴
10
`\[0,1,2,3,4,5,6,7\]` を元に `\[-3,-2,-1,1,2,3\]` を生成するコードに別のコードが混在していた問題を修正
answer
CHANGED
@@ -61,7 +61,6 @@
|
|
61
61
|
var halfLength = maxInt - minInt + 1,
|
62
62
|
array = [...Array(halfLength * 2 + 1).keys()].map(value => value - halfLength);
|
63
63
|
|
64
|
-
array = array.slice().reverse().map(value => value * -1).concat(array);
|
65
64
|
return array.splice(halfLength, 1), array[Math.floor(Math.random() * array.length)];
|
66
65
|
}
|
67
66
|
|
@@ -76,15 +75,6 @@
|
|
76
75
|
console.log(createRandomInt2(1, 3));
|
77
76
|
console.log(createRandomInt2(1, 3));
|
78
77
|
console.log(createRandomInt2(1, 3));
|
79
|
-
console.log(createRandomInt(1, 3));
|
80
|
-
console.log(createRandomInt(1, 3));
|
81
|
-
console.log(createRandomInt(1, 3));
|
82
|
-
console.log(createRandomInt(1, 3));
|
83
|
-
console.log(createRandomInt(1, 3));
|
84
|
-
console.log(createRandomInt(1, 3));
|
85
|
-
console.log(createRandomInt(1, 3));
|
86
|
-
console.log(createRandomInt(1, 3));
|
87
|
-
console.log(createRandomInt(1, 3));
|
88
78
|
```
|
89
79
|
|
90
80
|
### 数学的にエレガントなアルゴリズム
|
9
配列からランダムに値を得るコードの別解を追記
answer
CHANGED
@@ -16,21 +16,66 @@
|
|
16
16
|
|
17
17
|
[-3,-2,-1,0,1,2,3] からランダムに値を得る方法はシンプルですが、配列を生成するコストがかかるのでエレガントではありません。
|
18
18
|
|
19
|
+
### 配列からランダムに値を得るコード
|
20
|
+
|
21
|
+
`[1,2,3]` を元に `[-3,-2,-1,1,2,3]` を生成するコード。
|
22
|
+
|
19
23
|
```JavaScript
|
20
|
-
function
|
24
|
+
function createIntArray1 (minInt, maxInt) {
|
21
25
|
var array = [...Array(maxInt + 1).keys()].slice(minInt);
|
22
26
|
|
23
27
|
return array.slice().reverse().map(value => value * -1).concat(array);
|
24
28
|
}
|
25
29
|
|
26
|
-
function
|
30
|
+
function createRandomInt1 (minInt, maxInt) {
|
27
31
|
var array = [...Array(maxInt + 1).keys()].slice(minInt);
|
28
32
|
|
29
33
|
array = array.slice().reverse().map(value => value * -1).concat(array);
|
30
34
|
return array[Math.floor(Math.random() * array.length)];
|
31
35
|
}
|
32
36
|
|
33
|
-
console.log(JSON.stringify(
|
37
|
+
console.log(JSON.stringify(createIntArray1(1, 3))); // [-3,-2,-1,1,2,3]
|
38
|
+
console.log(createRandomInt1(1, 3));
|
39
|
+
console.log(createRandomInt1(1, 3));
|
40
|
+
console.log(createRandomInt1(1, 3));
|
41
|
+
console.log(createRandomInt1(1, 3));
|
42
|
+
console.log(createRandomInt1(1, 3));
|
43
|
+
console.log(createRandomInt1(1, 3));
|
44
|
+
console.log(createRandomInt1(1, 3));
|
45
|
+
console.log(createRandomInt1(1, 3));
|
46
|
+
console.log(createRandomInt1(1, 3));
|
47
|
+
console.log(createRandomInt1(1, 3));
|
48
|
+
```
|
49
|
+
|
50
|
+
`[0,1,2,3,4,5,6,7]` を元に `[-3,-2,-1,1,2,3]` を生成するコード。
|
51
|
+
|
52
|
+
```JavaScript
|
53
|
+
function createIntArray2 (minInt, maxInt) {
|
54
|
+
var halfLength = maxInt - minInt + 1,
|
55
|
+
array = [...Array(halfLength * 2 + 1).keys()].map(value => value - halfLength);
|
56
|
+
|
57
|
+
return array.splice(halfLength, 1), array;
|
58
|
+
}
|
59
|
+
|
60
|
+
function createRandomInt2 (minInt, maxInt) {
|
61
|
+
var halfLength = maxInt - minInt + 1,
|
62
|
+
array = [...Array(halfLength * 2 + 1).keys()].map(value => value - halfLength);
|
63
|
+
|
64
|
+
array = array.slice().reverse().map(value => value * -1).concat(array);
|
65
|
+
return array.splice(halfLength, 1), array[Math.floor(Math.random() * array.length)];
|
66
|
+
}
|
67
|
+
|
68
|
+
console.log(JSON.stringify(createIntArray2(1, 3))); // [-3,-2,-1,1,2,3]
|
69
|
+
console.log(createRandomInt2(1, 3));
|
70
|
+
console.log(createRandomInt2(1, 3));
|
71
|
+
console.log(createRandomInt2(1, 3));
|
72
|
+
console.log(createRandomInt2(1, 3));
|
73
|
+
console.log(createRandomInt2(1, 3));
|
74
|
+
console.log(createRandomInt2(1, 3));
|
75
|
+
console.log(createRandomInt2(1, 3));
|
76
|
+
console.log(createRandomInt2(1, 3));
|
77
|
+
console.log(createRandomInt2(1, 3));
|
78
|
+
console.log(createRandomInt2(1, 3));
|
34
79
|
console.log(createRandomInt(1, 3));
|
35
80
|
console.log(createRandomInt(1, 3));
|
36
81
|
console.log(createRandomInt(1, 3));
|
@@ -40,11 +85,8 @@
|
|
40
85
|
console.log(createRandomInt(1, 3));
|
41
86
|
console.log(createRandomInt(1, 3));
|
42
87
|
console.log(createRandomInt(1, 3));
|
43
|
-
console.log(createRandomInt(1, 3));
|
44
88
|
```
|
45
89
|
|
46
|
-
上記コードは配列を使ったコードとしてはエレガントですが、数学的ではないと思います。
|
47
|
-
|
48
90
|
### 数学的にエレガントなアルゴリズム
|
49
91
|
|
50
92
|
下記アルゴリズムは miu_ras さんが質問文中で「エレガントではない」としていますが、数学的に解決する方法の一つだと私は思います。
|
@@ -84,5 +126,6 @@
|
|
84
126
|
- 2017/02/03 19:44 算術演算子型コードを追記
|
85
127
|
- 2017/02/03 21:30 「数学的にエレガント」の定義に言及
|
86
128
|
- 2017/02/03 21:41 配列からランダムに値を得るコードを追記
|
129
|
+
- 2017/02/03 22:21 配列からランダムに値を得るコードの別解を追記
|
87
130
|
|
88
131
|
Re: miu_ras さん
|
8
0の例外処理について表現を修正
answer
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
「miu_ras さんがエレガントと思うコードの定義」が不明なので、私がエレガントと思うコードの定義を書きます。
|
4
4
|
|
5
|
-
**(1) [-3,-2,-1,0,1,2,3] から 0 を
|
5
|
+
**(1) [-3,-2,-1,0,1,2,3] からランダムに値を得てから 0 を例外処理するコードはエレガントではない**
|
6
6
|
|
7
7
|
0 を除外するコードは2種類考えられます。
|
8
8
|
|
7
配列からランダムに値を得るコードを追記
answer
CHANGED
@@ -23,8 +23,24 @@
|
|
23
23
|
return array.slice().reverse().map(value => value * -1).concat(array);
|
24
24
|
}
|
25
25
|
|
26
|
+
function createRandomInt (minInt, maxInt) {
|
27
|
+
var array = [...Array(maxInt + 1).keys()].slice(minInt);
|
28
|
+
|
29
|
+
array = array.slice().reverse().map(value => value * -1).concat(array);
|
30
|
+
return array[Math.floor(Math.random() * array.length)];
|
31
|
+
}
|
32
|
+
|
26
33
|
console.log(JSON.stringify(createIntArray(1, 3))); // [-3,-2,-1,1,2,3]
|
27
|
-
console.log(
|
34
|
+
console.log(createRandomInt(1, 3));
|
35
|
+
console.log(createRandomInt(1, 3));
|
36
|
+
console.log(createRandomInt(1, 3));
|
37
|
+
console.log(createRandomInt(1, 3));
|
38
|
+
console.log(createRandomInt(1, 3));
|
39
|
+
console.log(createRandomInt(1, 3));
|
40
|
+
console.log(createRandomInt(1, 3));
|
41
|
+
console.log(createRandomInt(1, 3));
|
42
|
+
console.log(createRandomInt(1, 3));
|
43
|
+
console.log(createRandomInt(1, 3));
|
28
44
|
```
|
29
45
|
|
30
46
|
上記コードは配列を使ったコードとしてはエレガントですが、数学的ではないと思います。
|
@@ -67,5 +83,6 @@
|
|
67
83
|
|
68
84
|
- 2017/02/03 19:44 算術演算子型コードを追記
|
69
85
|
- 2017/02/03 21:30 「数学的にエレガント」の定義に言及
|
86
|
+
- 2017/02/03 21:41 配列からランダムに値を得るコードを追記
|
70
87
|
|
71
88
|
Re: miu_ras さん
|
6
数学的にエレガントなコードで引数を解釈していなかった不具合を修正
answer
CHANGED
@@ -43,7 +43,7 @@
|
|
43
43
|
|
44
44
|
```JavaScript
|
45
45
|
function createRandomInt (minInt, maxInt) {
|
46
|
-
return (Math.round(Math.random()) * 2 - 1) * (Math.floor(Math.random() *
|
46
|
+
return (Math.round(Math.random()) * 2 - 1) * (Math.floor(Math.random() * (maxInt - minInt + 1)) + minInt);
|
47
47
|
}
|
48
48
|
|
49
49
|
console.log(createRandomInt(1, 3));
|
5
「数学的にエレガント」の定義に言及
answer
CHANGED
@@ -1,32 +1,71 @@
|
|
1
|
-
###
|
1
|
+
### 数学的にエレガントとは
|
2
2
|
|
3
|
+
「miu_ras さんがエレガントと思うコードの定義」が不明なので、私がエレガントと思うコードの定義を書きます。
|
4
|
+
|
5
|
+
**(1) [-3,-2,-1,0,1,2,3] から 0 を取り除くコードはエレガントではない**
|
6
|
+
|
7
|
+
0 を除外するコードは2種類考えられます。
|
8
|
+
|
9
|
+
- 0 が返された場合、1 を返す
|
10
|
+
- 0 が返された場合、もう一度、[-3,-2,-1,0,1,2,3] からランダムに値を得る(0 が返されなくなるまで繰り返す)
|
11
|
+
|
12
|
+
前者は 1 が返される確率が上がる為、エレガントではありません。
|
13
|
+
後者は 0 が返された場合の処理コストが上がる為、エレガントではありません。
|
14
|
+
|
15
|
+
**(2) 配列からランダムに値を得る方法はエレガントではない**
|
16
|
+
|
17
|
+
[-3,-2,-1,0,1,2,3] からランダムに値を得る方法はシンプルですが、配列を生成するコストがかかるのでエレガントではありません。
|
18
|
+
|
19
|
+
```JavaScript
|
20
|
+
function createIntArray (minInt, maxInt) {
|
21
|
+
var array = [...Array(maxInt + 1).keys()].slice(minInt);
|
22
|
+
|
23
|
+
return array.slice().reverse().map(value => value * -1).concat(array);
|
24
|
+
}
|
25
|
+
|
26
|
+
console.log(JSON.stringify(createIntArray(1, 3))); // [-3,-2,-1,1,2,3]
|
27
|
+
console.log(JSON.stringify(createIntArray(3, 9))); // [-9,-8,-7,-6,-5,-4,-3,3,4,5,6,7,8,9]
|
28
|
+
```
|
29
|
+
|
30
|
+
上記コードは配列を使ったコードとしてはエレガントですが、数学的ではないと思います。
|
31
|
+
|
32
|
+
### 数学的にエレガントなアルゴリズム
|
33
|
+
|
34
|
+
下記アルゴリズムは miu_ras さんが質問文中で「エレガントではない」としていますが、数学的に解決する方法の一つだと私は思います。
|
35
|
+
|
3
36
|
1. `Math.random()` で 1,2,3 の乱数を得る
|
4
37
|
2. `Math.random()` で -1,1 の乱数を得る
|
5
38
|
3. 1. と 2. の積を求める
|
6
39
|
|
7
|
-
### コード
|
40
|
+
### 数学的にエレガントなコード
|
8
41
|
|
9
|
-
時間的都合により未検証です。
|
10
|
-
エ
|
42
|
+
私が考える数学的にエレガントなコードは「`Math.xxxx` メソッド、算術演算子だけで完結するコード」です。
|
11
43
|
|
12
44
|
```JavaScript
|
13
|
-
|
45
|
+
function createRandomInt (minInt, maxInt) {
|
14
|
-
* 配列利用型
|
15
|
-
*/
|
16
|
-
|
46
|
+
return (Math.round(Math.random()) * 2 - 1) * (Math.floor(Math.random() * 3) + 1);
|
47
|
+
}
|
17
48
|
|
18
|
-
/**
|
19
|
-
* 算術演算子型
|
20
|
-
*/
|
21
|
-
|
49
|
+
console.log(createRandomInt(1, 3));
|
50
|
+
console.log(createRandomInt(1, 3));
|
51
|
+
console.log(createRandomInt(1, 3));
|
52
|
+
console.log(createRandomInt(1, 3));
|
53
|
+
console.log(createRandomInt(1, 3));
|
54
|
+
console.log(createRandomInt(1, 3));
|
55
|
+
console.log(createRandomInt(1, 3));
|
56
|
+
console.log(createRandomInt(1, 3));
|
57
|
+
console.log(createRandomInt(1, 3));
|
58
|
+
console.log(createRandomInt(1, 3));
|
22
59
|
```
|
23
60
|
|
24
|
-
|
61
|
+
`Math.random()` に偏りがない前提で考えるならば、
|
25
62
|
|
26
|
-
「数
|
63
|
+
- 「正の数/負の数」からランダムに偏りなく選びます
|
64
|
+
- 「1,2,3」からランダムに偏りなく選びます
|
27
65
|
|
28
66
|
### 更新履歴
|
29
67
|
|
30
68
|
- 2017/02/03 19:44 算術演算子型コードを追記
|
69
|
+
- 2017/02/03 21:30 「数学的にエレガント」の定義に言及
|
31
70
|
|
32
71
|
Re: miu_ras さん
|
4
markdown修正
answer
CHANGED
@@ -23,7 +23,7 @@
|
|
23
23
|
|
24
24
|
> 「-3, -2, -1, 1, 2, 3」の6つの整数をランダムで得る数学的な方法、
|
25
25
|
|
26
|
-
「数学的な方法」が三項演算子や配列を使わず、**算術演算子とMath.
|
26
|
+
「数学的な方法」が三項演算子や配列を使わず、**算術演算子とMath.xxxxメソッドのみでコーディング**という意味なら、後者が該当すると思います。
|
27
27
|
|
28
28
|
### 更新履歴
|
29
29
|
|
3
算術演算子型コードを追記
answer
CHANGED
@@ -4,15 +4,29 @@
|
|
4
4
|
2. `Math.random()` で -1,1 の乱数を得る
|
5
5
|
3. 1. と 2. の積を求める
|
6
6
|
|
7
|
-
-1,1の乱数を得るロジックをどうするかが考えどころですが、シンプルに考えるなら `[-1,1]` からランダムに値を取得すればいいですね。
|
8
|
-
|
9
7
|
### コード
|
10
8
|
|
11
9
|
時間的都合により未検証です。
|
12
10
|
エラーがあったらアルゴリズムから読みとってください…。後で修正します。
|
13
11
|
|
14
12
|
```JavaScript
|
13
|
+
/**
|
14
|
+
* 配列利用型
|
15
|
+
*/
|
15
16
|
[-1, 1][Math.round(Math.random())] * (Math.floor(Math.random() * 3) + 1);
|
17
|
+
|
18
|
+
/**
|
19
|
+
* 算術演算子型
|
20
|
+
*/
|
21
|
+
(Math.round(Math.random()) * 2 - 1) * (Math.floor(Math.random() * 3) + 1);
|
16
22
|
```
|
17
23
|
|
24
|
+
> 「-3, -2, -1, 1, 2, 3」の6つの整数をランダムで得る数学的な方法、
|
25
|
+
|
26
|
+
「数学的な方法」が三項演算子や配列を使わず、**算術演算子とMath.***メソッドのみでコーディング**という意味なら、後者が該当すると思います。
|
27
|
+
|
28
|
+
### 更新履歴
|
29
|
+
|
30
|
+
- 2017/02/03 19:44 算術演算子型コードを追記
|
31
|
+
|
18
32
|
Re: miu_ras さん
|
2
コード追記
answer
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
### アルゴリズム
|
2
2
|
|
3
3
|
1. `Math.random()` で 1,2,3 の乱数を得る
|
4
4
|
2. `Math.random()` で -1,1 の乱数を得る
|
@@ -6,4 +6,13 @@
|
|
6
6
|
|
7
7
|
-1,1の乱数を得るロジックをどうするかが考えどころですが、シンプルに考えるなら `[-1,1]` からランダムに値を取得すればいいですね。
|
8
8
|
|
9
|
+
### コード
|
10
|
+
|
11
|
+
時間的都合により未検証です。
|
12
|
+
エラーがあったらアルゴリズムから読みとってください…。後で修正します。
|
13
|
+
|
14
|
+
```JavaScript
|
15
|
+
[-1, 1][Math.round(Math.random())] * (Math.floor(Math.random() * 3) + 1);
|
16
|
+
```
|
17
|
+
|
9
18
|
Re: miu_ras さん
|
1
markdown修正
answer
CHANGED
@@ -4,6 +4,6 @@
|
|
4
4
|
2. `Math.random()` で -1,1 の乱数を得る
|
5
5
|
3. 1. と 2. の積を求める
|
6
6
|
|
7
|
-
|
7
|
+
-1,1の乱数を得るロジックをどうするかが考えどころですが、シンプルに考えるなら `[-1,1]` からランダムに値を取得すればいいですね。
|
8
8
|
|
9
9
|
Re: miu_ras さん
|