Using:
value = arc4random() % x
How can I avoid or eliminate modulo bias?
At least according to Wikipedia, modulo bias is an issue when programming games of chance.
Use arc4random_uniform(x). This does it for you.
arc4random_uniform(x)
According to the man page:
arc4random_uniform() will return a uniformly distributed random number less than upper_bound. arc4random_uniform() is recommended over constructions like arc4random() % upper_bound as it avoids "modulo bias" when the upper bound is not a power of two.
arc4random_uniform()
upper_bound
arc4random() % upper_bound
2.1m questions
2.1m answers
60 comments
57.0k users