Arc4random non-modulo bias?

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

+4
source share
2 answers

Although I can’t say it definitively, it seems to be a β€œproblem” for sowing. Calling arc4random () several times in milliseconds from each other will have a very high bias towards certain numbers. acr4random_uniform seems to solve this problem - it seems to be seeded with a more "reliable" manner

+1
source

Use acr4random_unform(3) to avoid modulo bias

Read this article: http://nshipster.com/random/

-one
source

Source: https://habr.com/ru/post/1497595/


All Articles