سؤال

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?

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

Use acr4random_unform(3) to avoid modulo bias

Read This Article:- http://nshipster.com/random/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top