arc4random和arc4random_uniform有什么区别? [重复]

时间:2022-01-11 03:40:11

This question already has an answer here:

这个问题在这里已有答案:

So I've seen old posts about the difference between random and arc4random in Obj. C, and I've seen answers to this online but I didn't really understand the answers so I was hoping someone here could explain it in an easier to understand manner.

所以我看过关于Obj中随机和arc4random之间差异的旧帖子。 C,我在网上看到过这个问题的答案,但我并不真正理解答案,所以我希望有人能以更容易理解的方式解释它。

What is the difference between using arc4random and arc4random_uniform to generate random numbers?

使用arc4random和arc4random_uniform生成随机数有什么区别?

1 个解决方案

#1


72  

arc4random returns an integer between 0 and (2^32)-1 while arc4random_uniform returns an integer between 0 and the upper bound you pass it.

arc4random返回0到(2 ^ 32)-1之间的整数,而arc4random_uniform返回0和你传递的上限之间的整数。

From man 3 arc4random:

来自man 3 arc4random:

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_uniform()建议使用像``arc4random()%upper_bound''这样的结构,因为当上限不是2的幂时,它避免了“模偏差”。

For example if you want an integer between 0 and 4 you could use

例如,如果你想要一个0到4之间的整数,你可以使用

arc4random() % 5

or

要么

arc4random_uniform(5)

Using the modulus operator in this case introduces modulo bias, so it's better to use arc4random_uniform.

在这种情况下使用模数运算符会引入模偏差,因此最好使用arc4random_uniform。

To understand modulo bias assume that arc4random had a much smaller range. Instead of 0 to (2^32) -1, it was 0 to (2^4) -1. If you perform % 5 on each number in that range you will get 0 four times, and 1, 2, 3 and 4 three times each making 0 more likely to occur. This difference becomes less significant when the range is much larger, but it's still better to avoid using modulus.

为了理解模偏差,假设arc4random具有更小的范围。而不是0到(2 ^ 32)-1,它是0到(2 ^ 4)-1。如果你对该范围内的每个数字执行%5,你将得到0四次,并且每次得到1,2,3和4次,使得0更有可能发生。当范围大得多时,这种差异变得不那么显着,但是避免使用模数仍然更好。

#1


72  

arc4random returns an integer between 0 and (2^32)-1 while arc4random_uniform returns an integer between 0 and the upper bound you pass it.

arc4random返回0到(2 ^ 32)-1之间的整数,而arc4random_uniform返回0和你传递的上限之间的整数。

From man 3 arc4random:

来自man 3 arc4random:

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_uniform()建议使用像``arc4random()%upper_bound''这样的结构,因为当上限不是2的幂时,它避免了“模偏差”。

For example if you want an integer between 0 and 4 you could use

例如,如果你想要一个0到4之间的整数,你可以使用

arc4random() % 5

or

要么

arc4random_uniform(5)

Using the modulus operator in this case introduces modulo bias, so it's better to use arc4random_uniform.

在这种情况下使用模数运算符会引入模偏差,因此最好使用arc4random_uniform。

To understand modulo bias assume that arc4random had a much smaller range. Instead of 0 to (2^32) -1, it was 0 to (2^4) -1. If you perform % 5 on each number in that range you will get 0 four times, and 1, 2, 3 and 4 three times each making 0 more likely to occur. This difference becomes less significant when the range is much larger, but it's still better to avoid using modulus.

为了理解模偏差,假设arc4random具有更小的范围。而不是0到(2 ^ 32)-1,它是0到(2 ^ 4)-1。如果你对该范围内的每个数字执行%5,你将得到0四次,并且每次得到1,2,3和4次,使得0更有可能发生。当范围大得多时,这种差异变得不那么显着,但是避免使用模数仍然更好。