Using
value = arc4random() % x
How can I avoid or eliminate modulo bias?
Thanks all.
BTW, at least according to Wikipedia, modulo bias is an issue when programming games of chance.
Using
value = arc4random() % x
How can I avoid or eliminate modulo bias?
Thanks all.
BTW, at least according to Wikipedia, modulo bias is an issue when programming games of chance.
Use the method below. It avoids "modulo bias" and it`s fast on iphone . Save you some cpu cycles.
IF you want 4-7:
OR if you want 0-8
although unless you are using any x under a million (or more) I wouldn't worry about it
Use
arc4random_uniform(x)
. This does it for you.According to the man page:
Somewhat pedantic objection to cobbal's answer. It "works", that is it removes the modulo bias, but it rejects more values than are necessary. The most extreme case is x = 2^31. All values of arc4random() should be accepted here but the code as written will reject half of them.
Instead, add 1 to the initialization of maxValue (that puts it at 2^32 so you'll have to use a 64 bit int), and then it's right. You can also avoid using a 64 bit int. Test beforehand if 2^32 % x == 0, if so all arc4random() values are acceptable and you can skip the loop, otherwise you can keep maxValue at 32 bits by subtracting 2^32 % x on initialization.
arc4random returns a 32-bit unsigned integer (0 to 232-1).
There will probably be no noticable modulo bias for small enough x. However, if you want to be really sure, do this:
y = 2p where 2p-1 < x ≤ 2p
If the maximum value of
arc4random
modx
is greater thanx
, ignore any values larger than the largestarc4random-max mod x
, callingarc4random
again instead.