Swift Int.randomの連続具合
概要
1〜2000のランダム値の連続具合を調べる。
ソース
Playgroundで実行
import Foundation for _ in 1...20 { var select = [Int](repeating:0, count:2000) var befor = -1 for _ in 1...2000 { let selectNo = Int.random(in: 1...2000) select[selectNo - 1] = select[selectNo - 1] + 1 if befor == selectNo { print("same:\(selectNo)") } befor = selectNo } print("0 : \(select.filter({i in i == 0}).count) 2:\(select.filter({i in i == 2}).count) >=3:\(select.filter({i in i >= 3}).count)") }
連続しても2回、3回続くことはなかった
結果
same:1353 same:1128 same:1163 0 : 751 2:365 >=3:170 same:1816 0 : 734 2:358 >=3:163 same:313 same:644 0 : 742 2:382 >=3:164 same:645 same:1363 0 : 723 2:358 >=3:153 0 : 750 2:352 >=3:166 same:899 0 : 732 2:370 >=3:160 same:1291 same:208 same:1606 0 : 717 2:347 >=3:162 same:1373 0 : 723 2:366 >=3:153 0 : 739 2:385 >=3:157 same:1411 same:332 0 : 749 2:382 >=3:156 same:1399 same:1237 same:1800 0 : 711 2:354 >=3:159 same:258 0 : 730 2:352 >=3:165 same:868 same:192 0 : 728 2:359 >=3:161 0 : 746 2:385 >=3:159 0 : 712 2:359 >=3:159 same:1727 0 : 762 2:377 >=3:167 same:1768 0 : 755 2:360 >=3:171 same:1045 same:273 0 : 746 2:364 >=3:168 same:485 0 : 718 2:364 >=3:156 same:320 same:1779 same:1135 0 : 728 2:350 >=3:166