Search code examples
objective-carc4random

arc4random non-modulo bias?


I am using arc4random() %2 in my code. It is called by 3 classes upon initialization, which happens in quick succession. However, approximately 70% plus of the results are always either all 0 or all 1 (the distribution between the 2 sets of 000 or 111 seems average). I would have expected a fairly random distribution of 1 and 0 between the 3 classes. How can this be explained?


Solution

  • Although I cannot say this definitively, it does appear to be a seeding "issue". Calling arc4random() multiple times within milliseconds of each other will have a very high bias towards certain numbers. acr4random_uniform seems to solve this problem - seemingly it is seeded in a more "robust" manner