Objective-C: Modulo bias
u_int32_t maxValue = ~((u_int32_t) 0); // equal to 0xffff...
maxValue -= maxValue % x; // make maxValue a multiple of x
while((value = arc4random()) >= maxValue) { // loop until we get 0 ≤ value < maxValue
}
value %= x;
although unless you are using any x under a million (or more) I wouldn't worry about it
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
val = arc4random() % y;
while(val >= x)
val = arc4random() % y;
Use arc4random_uniform(x)
. This does it for you.
According to the man page:
arc4random_uniform()
will return a uniformly distributed random number less thanupper_bound
.arc4random_uniform()
is recommended over constructions likearc4random() % upper_bound
as it avoids "modulo bias" when the upper bound is not a power of two.