回答編集履歴
4
説明
answer
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
無理やりやってみました。
|
2
|
+
引き直しなど条件分岐は行わず、
|
3
|
+
[0,5]の整数の一様分布に対してちゃんと均等な出現確率になるようになっています(多分)
|
2
4
|
|
3
5
|
```javascript
|
4
6
|
function f(t){
|
3
修正
answer
CHANGED
@@ -12,9 +12,9 @@
|
|
12
12
|
|
13
13
|
console.log([0,1,2,3,4,5].map(f))
|
14
14
|
|
15
|
-
|
15
|
+
histogram=[0,0,0,0,0,0,0]
|
16
16
|
for(i=0;i<1000;i++){
|
17
|
-
|
17
|
+
histogram[g()+3] ++
|
18
18
|
}
|
19
|
-
console.log(
|
19
|
+
console.log(histogram)
|
20
20
|
```
|
2
さらに改良
answer
CHANGED
@@ -5,13 +5,16 @@
|
|
5
5
|
return Math.ceil(Math.abs(t-2.5))*Math.sign(t-2.5)
|
6
6
|
}
|
7
7
|
|
8
|
+
function g(){
|
9
|
+
r = Math.floor(Math.random() * 6)
|
10
|
+
return f(r)
|
11
|
+
}
|
8
12
|
|
9
13
|
console.log([0,1,2,3,4,5].map(f))
|
10
14
|
|
11
15
|
arr=[0,0,0,0,0,0,0]
|
12
16
|
for(i=0;i<1000;i++){
|
13
|
-
r = Math.floor(Math.random() * 6)
|
14
|
-
arr[
|
17
|
+
arr[g()+3] ++
|
15
18
|
}
|
16
19
|
console.log(arr)
|
17
20
|
```
|
1
コード改良
answer
CHANGED
@@ -2,15 +2,16 @@
|
|
2
2
|
|
3
3
|
```javascript
|
4
4
|
function f(t){
|
5
|
-
return Math.ceil(Math.abs(t-2.5))*
|
5
|
+
return Math.ceil(Math.abs(t-2.5))*Math.sign(t-2.5)
|
6
6
|
}
|
7
7
|
|
8
8
|
|
9
|
-
console.log([0,1,2,3,4,5].map(f))
|
9
|
+
console.log([0,1,2,3,4,5].map(f))
|
10
10
|
|
11
|
-
|
11
|
+
arr=[0,0,0,0,0,0,0]
|
12
|
-
for(i=0;i<
|
12
|
+
for(i=0;i<1000;i++){
|
13
13
|
r = Math.floor(Math.random() * 6)
|
14
|
-
|
14
|
+
arr[f(r)+3] ++
|
15
15
|
}
|
16
|
+
console.log(arr)
|
16
17
|
```
|