LoginSignup
2
1

More than 5 years have passed since last update.

Swift Int.randomの分散具合

Posted at

概要

1〜2000のランダム値の分散具合を調べる。

ソース

Playgroundで実行

import Foundation

for _ in 1...20 {
    var select = [Int](repeating:0, count:2000)
    for _ in 1...2000 {
        let selectNo = Int.random(in: 1...2000)
        select[selectNo - 1] = select[selectNo - 1] + 1
    }
    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)")
}

選択されないものが700くらい。
意外と分散されない

結果

0 : 734 2:376 >=3:152
0 : 733 2:365 >=3:158
0 : 764 2:397 >=3:162
0 : 766 2:379 >=3:172
0 : 735 2:374 >=3:156
0 : 729 2:360 >=3:163
0 : 743 2:389 >=3:159
0 : 728 2:335 >=3:176
0 : 752 2:384 >=3:165
0 : 740 2:374 >=3:163
0 : 727 2:358 >=3:163
0 : 747 2:381 >=3:164
0 : 750 2:363 >=3:164
0 : 758 2:363 >=3:174
0 : 722 2:374 >=3:152
0 : 743 2:356 >=3:166
0 : 739 2:346 >=3:168
0 : 721 2:357 >=3:162
0 : 749 2:354 >=3:171
0 : 743 2:390 >=3:159
2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1