回答編集履歴

4

説明

2017/02/03 00:32

投稿

ozwk
ozwk

スコア13528

test CHANGED
@@ -1,4 +1,8 @@
1
1
  無理やりやってみました。
2
+
3
+ 引き直しなど条件分岐は行わず、
4
+
5
+ [0,5]の整数の一様分布に対してちゃんと均等な出現確率になるようになっています(多分)
2
6
 
3
7
 
4
8
 

3

修正

2017/02/03 00:32

投稿

ozwk
ozwk

スコア13528

test CHANGED
@@ -26,14 +26,14 @@
26
26
 
27
27
 
28
28
 
29
- arr=[0,0,0,0,0,0,0]
29
+ histogram=[0,0,0,0,0,0,0]
30
30
 
31
31
  for(i=0;i<1000;i++){
32
32
 
33
- arr[g()+3] ++
33
+ histogram[g()+3] ++
34
34
 
35
35
  }
36
36
 
37
- console.log(arr)
37
+ console.log(histogram)
38
38
 
39
39
  ```

2

さらに改良

2017/02/03 00:26

投稿

ozwk
ozwk

スコア13528

test CHANGED
@@ -12,6 +12,14 @@
12
12
 
13
13
 
14
14
 
15
+ function g(){
16
+
17
+ r = Math.floor(Math.random() * 6)
18
+
19
+ return f(r)
20
+
21
+ }
22
+
15
23
 
16
24
 
17
25
  console.log([0,1,2,3,4,5].map(f))
@@ -22,9 +30,7 @@
22
30
 
23
31
  for(i=0;i<1000;i++){
24
32
 
25
- r = Math.floor(Math.random() * 6)
26
-
27
- arr[f(r)+3] ++
33
+ arr[g()+3] ++
28
34
 
29
35
  }
30
36
 

1

コード改良

2017/02/03 00:25

投稿

ozwk
ozwk

スコア13528

test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  function f(t){
8
8
 
9
- return Math.ceil(Math.abs(t-2.5))*(t-2.5)/Math.abs(t-2.5)
9
+ return Math.ceil(Math.abs(t-2.5))*Math.sign(t-2.5)
10
10
 
11
11
  }
12
12
 
@@ -14,18 +14,20 @@
14
14
 
15
15
 
16
16
 
17
- console.log([0,1,2,3,4,5].map(f)) //[-3,-2,-1,1,2,3]
17
+ console.log([0,1,2,3,4,5].map(f))
18
18
 
19
19
 
20
20
 
21
+ arr=[0,0,0,0,0,0,0]
21
22
 
22
-
23
- for(i=0;i<20;i++){
23
+ for(i=0;i<1000;i++){
24
24
 
25
25
  r = Math.floor(Math.random() * 6)
26
26
 
27
- console.log(f(r)) // -3,-2,-1,1,2,3 のどれか
27
+ arr[f(r)+3] ++
28
28
 
29
29
  }
30
30
 
31
+ console.log(arr)
32
+
31
33
  ```