回答編集履歴

10

`\[0,1,2,3,4,5,6,7\]` を元に `\[-3,-2,-1,1,2,3\]` を生成するコードに別のコードが混在していた問題を修正

2017/02/03 13:29

投稿

think49
think49

スコア18156

test CHANGED
@@ -124,8 +124,6 @@
124
124
 
125
125
 
126
126
 
127
- array = array.slice().reverse().map(value => value * -1).concat(array);
128
-
129
127
  return array.splice(halfLength, 1), array[Math.floor(Math.random() * array.length)];
130
128
 
131
129
  }
@@ -154,24 +152,6 @@
154
152
 
155
153
  console.log(createRandomInt2(1, 3));
156
154
 
157
- console.log(createRandomInt(1, 3));
158
-
159
- console.log(createRandomInt(1, 3));
160
-
161
- console.log(createRandomInt(1, 3));
162
-
163
- console.log(createRandomInt(1, 3));
164
-
165
- console.log(createRandomInt(1, 3));
166
-
167
- console.log(createRandomInt(1, 3));
168
-
169
- console.log(createRandomInt(1, 3));
170
-
171
- console.log(createRandomInt(1, 3));
172
-
173
- console.log(createRandomInt(1, 3));
174
-
175
155
  ```
176
156
 
177
157
 

9

配列からランダムに値を得るコードの別解を追記

2017/02/03 13:29

投稿

think49
think49

スコア18156

test CHANGED
@@ -34,9 +34,17 @@
34
34
 
35
35
 
36
36
 
37
+ ### 配列からランダムに値を得るコード
38
+
39
+
40
+
41
+ `[1,2,3]` を元に `[-3,-2,-1,1,2,3]` を生成するコード。
42
+
43
+
44
+
37
45
  ```JavaScript
38
46
 
39
- function createIntArray (minInt, maxInt) {
47
+ function createIntArray1 (minInt, maxInt) {
40
48
 
41
49
  var array = [...Array(maxInt + 1).keys()].slice(minInt);
42
50
 
@@ -48,21 +56,159 @@
48
56
 
49
57
 
50
58
 
59
+ function createRandomInt1 (minInt, maxInt) {
60
+
61
+ var array = [...Array(maxInt + 1).keys()].slice(minInt);
62
+
63
+
64
+
65
+ array = array.slice().reverse().map(value => value * -1).concat(array);
66
+
67
+ return array[Math.floor(Math.random() * array.length)];
68
+
69
+ }
70
+
71
+
72
+
73
+ console.log(JSON.stringify(createIntArray1(1, 3))); // [-3,-2,-1,1,2,3]
74
+
75
+ console.log(createRandomInt1(1, 3));
76
+
77
+ console.log(createRandomInt1(1, 3));
78
+
79
+ console.log(createRandomInt1(1, 3));
80
+
81
+ console.log(createRandomInt1(1, 3));
82
+
83
+ console.log(createRandomInt1(1, 3));
84
+
85
+ console.log(createRandomInt1(1, 3));
86
+
87
+ console.log(createRandomInt1(1, 3));
88
+
89
+ console.log(createRandomInt1(1, 3));
90
+
91
+ console.log(createRandomInt1(1, 3));
92
+
93
+ console.log(createRandomInt1(1, 3));
94
+
95
+ ```
96
+
97
+
98
+
99
+ `[0,1,2,3,4,5,6,7]` を元に `[-3,-2,-1,1,2,3]` を生成するコード。
100
+
101
+
102
+
103
+ ```JavaScript
104
+
105
+ function createIntArray2 (minInt, maxInt) {
106
+
107
+ var halfLength = maxInt - minInt + 1,
108
+
109
+ array = [...Array(halfLength * 2 + 1).keys()].map(value => value - halfLength);
110
+
111
+
112
+
113
+ return array.splice(halfLength, 1), array;
114
+
115
+ }
116
+
117
+
118
+
119
+ function createRandomInt2 (minInt, maxInt) {
120
+
121
+ var halfLength = maxInt - minInt + 1,
122
+
123
+ array = [...Array(halfLength * 2 + 1).keys()].map(value => value - halfLength);
124
+
125
+
126
+
127
+ array = array.slice().reverse().map(value => value * -1).concat(array);
128
+
129
+ return array.splice(halfLength, 1), array[Math.floor(Math.random() * array.length)];
130
+
131
+ }
132
+
133
+
134
+
135
+ console.log(JSON.stringify(createIntArray2(1, 3))); // [-3,-2,-1,1,2,3]
136
+
137
+ console.log(createRandomInt2(1, 3));
138
+
139
+ console.log(createRandomInt2(1, 3));
140
+
141
+ console.log(createRandomInt2(1, 3));
142
+
143
+ console.log(createRandomInt2(1, 3));
144
+
145
+ console.log(createRandomInt2(1, 3));
146
+
147
+ console.log(createRandomInt2(1, 3));
148
+
149
+ console.log(createRandomInt2(1, 3));
150
+
151
+ console.log(createRandomInt2(1, 3));
152
+
153
+ console.log(createRandomInt2(1, 3));
154
+
155
+ console.log(createRandomInt2(1, 3));
156
+
157
+ console.log(createRandomInt(1, 3));
158
+
159
+ console.log(createRandomInt(1, 3));
160
+
161
+ console.log(createRandomInt(1, 3));
162
+
163
+ console.log(createRandomInt(1, 3));
164
+
165
+ console.log(createRandomInt(1, 3));
166
+
167
+ console.log(createRandomInt(1, 3));
168
+
169
+ console.log(createRandomInt(1, 3));
170
+
171
+ console.log(createRandomInt(1, 3));
172
+
173
+ console.log(createRandomInt(1, 3));
174
+
175
+ ```
176
+
177
+
178
+
179
+ ### 数学的にエレガントなアルゴリズム
180
+
181
+
182
+
183
+ 下記アルゴリズムは miu_ras さんが質問文中で「エレガントではない」としていますが、数学的に解決する方法の一つだと私は思います。
184
+
185
+
186
+
187
+ 1. `Math.random()` で 1,2,3 の乱数を得る
188
+
189
+ 2. `Math.random()` で -1,1 の乱数を得る
190
+
191
+ 3. 1. と 2. の積を求める
192
+
193
+
194
+
195
+ ### 数学的にエレガントなコード
196
+
197
+
198
+
199
+ 私が考える数学的にエレガントなコードは「`Math.xxxx` メソッド、算術演算子だけで完結するコード」です。
200
+
201
+
202
+
203
+ ```JavaScript
204
+
51
205
  function createRandomInt (minInt, maxInt) {
52
206
 
53
- var array = [...Array(maxInt + 1).keys()].slice(minInt);
54
-
55
-
56
-
57
- array = array.slice().reverse().map(value => value * -1).concat(array);
58
-
59
- return array[Math.floor(Math.random() * array.length)];
207
+ return (Math.round(Math.random()) * 2 - 1) * (Math.floor(Math.random() * (maxInt - minInt + 1)) + minInt);
60
-
208
+
61
- }
209
+ }
62
-
63
-
64
-
65
- console.log(JSON.stringify(createIntArray(1, 3))); // [-3,-2,-1,1,2,3]
210
+
211
+
66
212
 
67
213
  console.log(createRandomInt(1, 3));
68
214
 
@@ -88,68 +234,6 @@
88
234
 
89
235
 
90
236
 
91
- 上記コードは配列を使ったコードとしてはエレガントですが、数学的ではないと思います。
92
-
93
-
94
-
95
- ### 数学的にエレガントなアルゴリズム
96
-
97
-
98
-
99
- 下記アルゴリズムは miu_ras さんが質問文中で「エレガントではない」としていますが、数学的に解決する方法の一つだと私は思います。
100
-
101
-
102
-
103
- 1. `Math.random()` で 1,2,3 の乱数を得る
104
-
105
- 2. `Math.random()` で -1,1 の乱数を得る
106
-
107
- 3. 1. と 2. の積を求める
108
-
109
-
110
-
111
- ### 数学的にエレガントなコード
112
-
113
-
114
-
115
- 私が考える数学的にエレガントなコードは「`Math.xxxx` メソッド、算術演算子だけで完結するコード」です。
116
-
117
-
118
-
119
- ```JavaScript
120
-
121
- function createRandomInt (minInt, maxInt) {
122
-
123
- return (Math.round(Math.random()) * 2 - 1) * (Math.floor(Math.random() * (maxInt - minInt + 1)) + minInt);
124
-
125
- }
126
-
127
-
128
-
129
- console.log(createRandomInt(1, 3));
130
-
131
- console.log(createRandomInt(1, 3));
132
-
133
- console.log(createRandomInt(1, 3));
134
-
135
- console.log(createRandomInt(1, 3));
136
-
137
- console.log(createRandomInt(1, 3));
138
-
139
- console.log(createRandomInt(1, 3));
140
-
141
- console.log(createRandomInt(1, 3));
142
-
143
- console.log(createRandomInt(1, 3));
144
-
145
- console.log(createRandomInt(1, 3));
146
-
147
- console.log(createRandomInt(1, 3));
148
-
149
- ```
150
-
151
-
152
-
153
237
  `Math.random()` に偏りがない前提で考えるならば、
154
238
 
155
239
 
@@ -170,6 +254,8 @@
170
254
 
171
255
  - 2017/02/03 21:41 配列からランダムに値を得るコードを追記
172
256
 
257
+ - 2017/02/03 22:21 配列からランダムに値を得るコードの別解を追記
258
+
173
259
 
174
260
 
175
261
  Re: miu_ras さん

8

0の例外処理について表現を修正

2017/02/03 13:22

投稿

think49
think49

スコア18156

test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
 
8
8
 
9
- **(1) [-3,-2,-1,0,1,2,3] から 0 を取り除くコードはエレガントではない**
9
+ **(1) [-3,-2,-1,0,1,2,3] からランダムに値を得てから 0 を例外処理するコードはエレガントではない**
10
10
 
11
11
 
12
12
 

7

配列からランダムに値を得るコードを追記

2017/02/03 12:51

投稿

think49
think49

スコア18156

test CHANGED
@@ -48,9 +48,41 @@
48
48
 
49
49
 
50
50
 
51
+ function createRandomInt (minInt, maxInt) {
52
+
53
+ var array = [...Array(maxInt + 1).keys()].slice(minInt);
54
+
55
+
56
+
57
+ array = array.slice().reverse().map(value => value * -1).concat(array);
58
+
59
+ return array[Math.floor(Math.random() * array.length)];
60
+
61
+ }
62
+
63
+
64
+
51
65
  console.log(JSON.stringify(createIntArray(1, 3))); // [-3,-2,-1,1,2,3]
52
66
 
53
- console.log(JSON.stringify(createIntArray(3, 9))); // [-9,-8,-7,-6,-5,-4,-3,3,4,5,6,7,8,9]
67
+ console.log(createRandomInt(1, 3));
68
+
69
+ console.log(createRandomInt(1, 3));
70
+
71
+ console.log(createRandomInt(1, 3));
72
+
73
+ console.log(createRandomInt(1, 3));
74
+
75
+ console.log(createRandomInt(1, 3));
76
+
77
+ console.log(createRandomInt(1, 3));
78
+
79
+ console.log(createRandomInt(1, 3));
80
+
81
+ console.log(createRandomInt(1, 3));
82
+
83
+ console.log(createRandomInt(1, 3));
84
+
85
+ console.log(createRandomInt(1, 3));
54
86
 
55
87
  ```
56
88
 
@@ -136,6 +168,8 @@
136
168
 
137
169
  - 2017/02/03 21:30 「数学的にエレガント」の定義に言及
138
170
 
171
+ - 2017/02/03 21:41 配列からランダムに値を得るコードを追記
172
+
139
173
 
140
174
 
141
175
  Re: miu_ras さん

6

数学的にエレガントなコードで引数を解釈していなかった不具合を修正

2017/02/03 12:42

投稿

think49
think49

スコア18156

test CHANGED
@@ -88,7 +88,7 @@
88
88
 
89
89
  function createRandomInt (minInt, maxInt) {
90
90
 
91
- return (Math.round(Math.random()) * 2 - 1) * (Math.floor(Math.random() * 3) + 1);
91
+ return (Math.round(Math.random()) * 2 - 1) * (Math.floor(Math.random() * (maxInt - minInt + 1)) + minInt);
92
92
 
93
93
  }
94
94
 

5

「数学的にエレガント」の定義に言及

2017/02/03 12:38

投稿

think49
think49

スコア18156

test CHANGED
@@ -1,4 +1,70 @@
1
+ ### 数学的にエレガントとは
2
+
3
+
4
+
5
+ 「miu_ras さんがエレガントと思うコードの定義」が不明なので、私がエレガントと思うコードの定義を書きます。
6
+
7
+
8
+
9
+ **(1) [-3,-2,-1,0,1,2,3] から 0 を取り除くコードはエレガントではない**
10
+
11
+
12
+
13
+ 0 を除外するコードは2種類考えられます。
14
+
15
+
16
+
17
+ - 0 が返された場合、1 を返す
18
+
19
+ - 0 が返された場合、もう一度、[-3,-2,-1,0,1,2,3] からランダムに値を得る(0 が返されなくなるまで繰り返す)
20
+
21
+
22
+
23
+ 前者は 1 が返される確率が上がる為、エレガントではありません。
24
+
25
+ 後者は 0 が返された場合の処理コストが上がる為、エレガントではありません。
26
+
27
+
28
+
29
+ **(2) 配列からランダムに値を得る方法はエレガントではない**
30
+
31
+
32
+
33
+ [-3,-2,-1,0,1,2,3] からランダムに値を得る方法はシンプルですが、配列を生成するコストがかかるのでエレガントではありません。
34
+
35
+
36
+
37
+ ```JavaScript
38
+
39
+ function createIntArray (minInt, maxInt) {
40
+
41
+ var array = [...Array(maxInt + 1).keys()].slice(minInt);
42
+
43
+
44
+
45
+ return array.slice().reverse().map(value => value * -1).concat(array);
46
+
47
+ }
48
+
49
+
50
+
51
+ console.log(JSON.stringify(createIntArray(1, 3))); // [-3,-2,-1,1,2,3]
52
+
53
+ console.log(JSON.stringify(createIntArray(3, 9))); // [-9,-8,-7,-6,-5,-4,-3,3,4,5,6,7,8,9]
54
+
55
+ ```
56
+
57
+
58
+
59
+ 上記コードは配列を使ったコードとしてはエレガントですが、数学的ではないと思います。
60
+
61
+
62
+
1
- ### アルゴリズム
63
+ ### 数学的にエレガントなアルゴリズム
64
+
65
+
66
+
67
+ 下記アルゴリズムは miu_ras さんが質問文中で「エレガントではない」としていますが、数学的に解決する方法の一つだと私は思います。
2
68
 
3
69
 
4
70
 
@@ -10,45 +76,55 @@
10
76
 
11
77
 
12
78
 
13
- ### コード
79
+ ### 数学的にエレガントなコード
14
80
 
15
81
 
16
82
 
17
- 時間的都合により未検証です。
18
-
19
- があったらアルゴリズムから読みとってくさい…。後修正します。
83
+ 私が考える数学的にレガントなコドは「`Math.xxxx` メソッド、算術演算子完結るコード」です
20
84
 
21
85
 
22
86
 
23
87
  ```JavaScript
24
88
 
25
- /**
89
+ function createRandomInt (minInt, maxInt) {
26
90
 
27
- * 配列利用型
91
+ return (Math.round(Math.random()) * 2 - 1) * (Math.floor(Math.random() * 3) + 1);
28
92
 
29
- */
93
+ }
30
-
31
- [-1, 1][Math.round(Math.random())] * (Math.floor(Math.random() * 3) + 1);
32
94
 
33
95
 
34
96
 
35
- /**
97
+ console.log(createRandomInt(1, 3));
36
98
 
37
- * 算術演算子型
99
+ console.log(createRandomInt(1, 3));
38
100
 
39
- */
101
+ console.log(createRandomInt(1, 3));
40
102
 
41
- (Math.round(Math.random()) * 2 - 1) * (Math.floor(Math.random() * 3) + 1);
103
+ console.log(createRandomInt(1, 3));
104
+
105
+ console.log(createRandomInt(1, 3));
106
+
107
+ console.log(createRandomInt(1, 3));
108
+
109
+ console.log(createRandomInt(1, 3));
110
+
111
+ console.log(createRandomInt(1, 3));
112
+
113
+ console.log(createRandomInt(1, 3));
114
+
115
+ console.log(createRandomInt(1, 3));
42
116
 
43
117
  ```
44
118
 
45
119
 
46
120
 
47
- > 「-3, -2, -1, 1, 2, 3」の6つの整数をランダム数学的方法
121
+ `Math.random()` に偏りがない前提考えるならば
48
122
 
49
123
 
50
124
 
51
- 「数学的な方法が三項演算子や配列を使わず、**算術演算子とMath.xxxxメソッドのみでコーディグ**という意味ら、後者が該当すると思います
125
+ - 正の/負の数からラダムに偏りく選びます
126
+
127
+ - 「1,2,3」からランダムに偏りなく選びます
52
128
 
53
129
 
54
130
 
@@ -58,6 +134,8 @@
58
134
 
59
135
  - 2017/02/03 19:44 算術演算子型コードを追記
60
136
 
137
+ - 2017/02/03 21:30 「数学的にエレガント」の定義に言及
138
+
61
139
 
62
140
 
63
141
  Re: miu_ras さん

4

markdown修正

2017/02/03 12:30

投稿

think49
think49

スコア18156

test CHANGED
@@ -48,7 +48,7 @@
48
48
 
49
49
 
50
50
 
51
- 「数学的な方法」が三項演算子や配列を使わず、**算術演算子とMath.***メソッドのみでコーディング**という意味なら、後者が該当すると思います。
51
+ 「数学的な方法」が三項演算子や配列を使わず、**算術演算子とMath.xxxxメソッドのみでコーディング**という意味なら、後者が該当すると思います。
52
52
 
53
53
 
54
54
 

3

算術演算子型コードを追記

2017/02/03 10:45

投稿

think49
think49

スコア18156

test CHANGED
@@ -7,10 +7,6 @@
7
7
  2. `Math.random()` で -1,1 の乱数を得る
8
8
 
9
9
  3. 1. と 2. の積を求める
10
-
11
-
12
-
13
- -1,1の乱数を得るロジックをどうするかが考えどころですが、シンプルに考えるなら `[-1,1]` からランダムに値を取得すればいいですね。
14
10
 
15
11
 
16
12
 
@@ -26,10 +22,42 @@
26
22
 
27
23
  ```JavaScript
28
24
 
25
+ /**
26
+
27
+ * 配列利用型
28
+
29
+ */
30
+
29
31
  [-1, 1][Math.round(Math.random())] * (Math.floor(Math.random() * 3) + 1);
32
+
33
+
34
+
35
+ /**
36
+
37
+ * 算術演算子型
38
+
39
+ */
40
+
41
+ (Math.round(Math.random()) * 2 - 1) * (Math.floor(Math.random() * 3) + 1);
30
42
 
31
43
  ```
32
44
 
33
45
 
34
46
 
47
+ > 「-3, -2, -1, 1, 2, 3」の6つの整数をランダムで得る数学的な方法、
48
+
49
+
50
+
51
+ 「数学的な方法」が三項演算子や配列を使わず、**算術演算子とMath.***メソッドのみでコーディング**という意味なら、後者が該当すると思います。
52
+
53
+
54
+
55
+ ### 更新履歴
56
+
57
+
58
+
59
+ - 2017/02/03 19:44 算術演算子型コードを追記
60
+
61
+
62
+
35
63
  Re: miu_ras さん

2

コード追記

2017/02/03 10:44

投稿

think49
think49

スコア18156

test CHANGED
@@ -1,4 +1,4 @@
1
- 今、コードを書ける状況にないのでアルゴリズムだけ。
1
+ ### アルゴリズム
2
2
 
3
3
 
4
4
 
@@ -14,4 +14,22 @@
14
14
 
15
15
 
16
16
 
17
+ ### コード
18
+
19
+
20
+
21
+ 時間的都合により未検証です。
22
+
23
+ エラーがあったらアルゴリズムから読みとってください…。後で修正します。
24
+
25
+
26
+
27
+ ```JavaScript
28
+
29
+ [-1, 1][Math.round(Math.random())] * (Math.floor(Math.random() * 3) + 1);
30
+
31
+ ```
32
+
33
+
34
+
17
35
  Re: miu_ras さん

1

markdown修正

2017/02/03 05:05

投稿

think49
think49

スコア18156

test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
 
12
12
 
13
- 2. をどうするかが考えどころですが、シンプルに考えるなら `[-1,1]` からランダムに値を取得すればいいですね。
13
+ -1,1の乱数得るロジックをどうするかが考えどころですが、シンプルに考えるなら `[-1,1]` からランダムに値を取得すればいいですね。
14
14
 
15
15
 
16
16