arc4random() 的最大 return 值
Maximum return value of arc4random()
出于某种原因,我找不到任何官方文档告诉我 arc4random()
的范围。我在一些非权威来源中看到它是 2^32 - 1,略高于 40 亿。
谁能证实这一点?如果是这样,您能否 link 到某个显示此数字的官方文档?我正在尝试在我的 iOS 应用程序上生成一个随机数,并且需要有一个相当正式的上限才能使算法正常工作。
你是对的,
DESCRIPTION
The arc4random() function uses the key stream generator employed by the arc4 cipher, which uses 8*8 8
bit S-Boxes. The S-Boxes can be in about (2**1700) states. The arc4random() function returns pseudo-random pseudorandom
random numbers in the range of 0 to (2**32)-1, and therefore has twice the range of rand(3) and
random(3).
arc4random_buf() function fills the region buf of length nbytes with
ARC4-derived random data.
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.
The arc4random_stir() function reads data from /dev/urandom and uses
it to permute the S-Boxes via arc4random_addrandom().
There is no need to call arc4random_stir() before using arc4random()
functions family, since they auto-matically automatically matically
initialize themselves.
The arc4random()
function returns pseudo-random numbers in the range of 0 to 232 – 1…
而且,令人高兴的是,这与 uint32_t
的 return 类型匹配。也就是说,arc4random
可能return任意32位无符号整数。包含的上限是UINT32_MAX
(或Swift中的UInt32.max
)。
但是,如果你只需要在特定范围内均匀分布的数字,你应该使用arc4random_uniform(n)
(范围是独占的:arc4random_uniform(5)
可以 return 0、1、2、3 或 4)。这更正确——也更方便! — 比使用 %
截断由 arc4random()
.
编辑的范围 return
出于某种原因,我找不到任何官方文档告诉我 arc4random()
的范围。我在一些非权威来源中看到它是 2^32 - 1,略高于 40 亿。
谁能证实这一点?如果是这样,您能否 link 到某个显示此数字的官方文档?我正在尝试在我的 iOS 应用程序上生成一个随机数,并且需要有一个相当正式的上限才能使算法正常工作。
你是对的,
DESCRIPTION
The arc4random() function uses the key stream generator employed by the arc4 cipher, which uses 8*8 8 bit S-Boxes. The S-Boxes can be in about (2**1700) states. The arc4random() function returns pseudo-random pseudorandom random numbers in the range of 0 to (2**32)-1, and therefore has twice the range of rand(3) and random(3).
arc4random_buf() function fills the region buf of length nbytes with ARC4-derived random data.
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.
The arc4random_stir() function reads data from /dev/urandom and uses it to permute the S-Boxes via arc4random_addrandom().
There is no need to call arc4random_stir() before using arc4random() functions family, since they auto-matically automatically matically initialize themselves.
The
arc4random()
function returns pseudo-random numbers in the range of 0 to 232 – 1…
而且,令人高兴的是,这与 uint32_t
的 return 类型匹配。也就是说,arc4random
可能return任意32位无符号整数。包含的上限是UINT32_MAX
(或Swift中的UInt32.max
)。
但是,如果你只需要在特定范围内均匀分布的数字,你应该使用arc4random_uniform(n)
(范围是独占的:arc4random_uniform(5)
可以 return 0、1、2、3 或 4)。这更正确——也更方便! — 比使用 %
截断由 arc4random()
.