Help us understand the problem. What is going on with this article?

Swift Int.randomの分散具合

More than 1 year has passed since last update.

概要

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
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした