Arc4random modulo biased

前端 未结 1 2058
不思量自难忘°
不思量自难忘° 2020-12-01 15:05

According to this documentation,

arc4random_uniform() is recommended over constructions like arc4random() % upper_bound as i

相关标签:
1条回答
  • 2020-12-01 15:23

    arc4random() returns an unsigned 32-bit integer, meaning the values are between 0 and 2^32-1 = 4 294 967 295.

    Now, the bias results from the fact that the multiple subintervals created with modulo are not fitting exactly into the random output range. Lets imagine for clarity a random generator that creates numbers from 0 to 198 inclusive. You want numbers from 0 to 99, therefore you calculate random() % 100, yielding 0 to 99:

    0 % 100 = 0
    99 % 100 = 99
    100 % 100 = 0
    198 % 100 = 98

    You see that 99 is the only number which can occur only once while all others can occur twice in a run. That means that the probability for 99 is exactly halved which is also the worst case in a bias where at least 2 subintervals are involved.
    As all powers of two smaller than the range interval fits nicely into the 2^32 interval, the bias disappears in this case.

    The implications are that the smaller the result set with modulo and the higher the random output range, the smaller the bias. In your example, 6 is your upper bound (I assume 0 is the lower bound), so you use % 7, resulting that 0-3 occurs 613 566 757 times while 4-6 occurs 613 566 756 times.
    So 0-3 is 613 566 757 / 613 566 756 = 1.0000000016298 times more probable than 4-6.

    While it seems easy to dismiss, some experiments (especially Monte-Carlo experiments) were flawed exactly because these seemingly incredible small differences were pretty important.

    Even worse is the bias if the desired output range is bigger than the random target range. Please read the Fisher-Yates shuffle entry because many poker sites learned the hard way that normal linear congruential random generators and bad shuffling algorithms resulted in impossible or very probable decks or worse, predictable decks.

    0 讨论(0)
提交回复
热议问题