[英]How do I seed the rand() function in Objective-C?
Part of what I'm developing is a random company name generator. 我正在开发的部分是一个随机的公司名称生成器。 It draws from several arrays of name parts.
它从几个名称部分数组中提取。 I use the
rand()
function to draw the random name parts. 我使用
rand()
函数绘制随机名称部分。 However, the same "random" numbers are always generated in the same sequence every time I launch the app, so the same names always appear. 但是, 每次启动应用程序时, 相同的“随机”数字始终以相同的顺序生成 ,因此始终显示相同的名称。
So I searched around SO, and in C there is an srand()
function to "seed" the random function with something like the current time to make it more random - like srand(time(NULL))
. 所以我在SO周围搜索,并且在C中有一个
srand()
函数,用随机函数“播种”当前时间以使其更随机 - 比如srand(time(NULL))
。 Is there something like that for Objective-C that I can use for iOS development? 我可以用于iOS开发的Objective-C有类似的东西吗?
Why don't you use arc4random
which doesn't require a seed? 你为什么不使用不需要种子的
arc4random
? You use it like this: 你这样使用它:
int r = arc4random();
Here's an article comparing it to rand()
. 这是一篇将它与
rand()
进行比较的文章。 The arc4random()
man page says this about it in comparison to rand()
: 与
rand()
相比, arc4random()
手册页说明了这一点:
The arc4random() function uses the key stream generator employed by the arc4 cipher, which uses 8*8 8 bit S-Boxes.
arc4random()函数使用arc4密码使用的密钥流生成器,它使用8 * 8 8位S-Box。 The S-Boxes can be in about (2 1700) states.
S-Box可以处于大约(2 1700)个状态。 The arc4random() function returns pseudo- random numbers in the range of 0 to (2 32)-1, and therefore has twice the range of rand(3) and random(3).
arc4random()函数返回0到(2 32)-1 范围内的伪随机数 ,因此具有rand(3)和random(3) 范围的两倍 。
If you want a random number within a range, you can use the arc4random_uniform()
function. 如果想要一个范围内的随机数,可以使用
arc4random_uniform()
函数。 For example, to generate a random number between 0 and 10, you would do this: 例如,要生成0到10之间的随机数,您可以这样做:
int i = arc4random_uniform(11);
Here's some info from the man page: 以下是手册页中的一些信息:
arc4random_uniform(upper_bound) will return a uniformly distributed random number less than upper_bound.
arc4random_uniform(upper_bound)将返回小于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()建议使用像``arc4random()%upper_bound''这样的结构,因为当上限不是2的幂时,它避免了“模偏差”。
The functions rand()
and srand()
are part of the Standard C Library and like the rest of the C library fully available for you to us in iOS development with Objective-C. 函数
rand()
和srand()
是标准C库的一部分,就像使用Objective-C在iOS开发中完全可用的C库的其余部分一样。 Note that these routines have been superseded by random()
and srandom()
, which have almost identically calling conventions to rand()
and srand()
but produce much better results with a larger period. 请注意,这些例程已被
random()
和srandom()
取代,它们对rand()
和srand()
具有几乎相同的调用约定,但在更长的周期内产生更好的结果。 There is also an srandomdev()
routine which initializes the state of the random number generator using the random number device. 还有一个
srandomdev()
例程,它使用随机数设备初始化随机数发生器的状态。 These are also part of the Standard C Library and available for use on iOS in Objective-C. 这些也是标准C库的一部分,可在iOS-Objective-C中使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.