如何在Objective-C中引入rand()函数?

时间:2022-09-07 08:47:13

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. However, the same "random" numbers are always generated in the same sequence every time I launch the app, so the same names always appear.

我正在开发的部分是一个随机的公司名称生成器。它从名称部分的几个数组中提取。我使用rand()函数来绘制随机名称部分。然而,每次我启动app时,同样的“随机数”总是以同样的顺序生成,所以总是会出现同样的名字。

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)). Is there something like that for Objective-C that I can use for iOS development?

所以我搜索了So,在C中有一个srand()函数来“播种”随机函数,比如当前时间,以使它更随机——比如srand(time(NULL))。对于Objective-C有类似的东西我可以用在iOS开发中吗?

2 个解决方案

#1


8  

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. 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. There is also an srandomdev() routine which initializes the state of the random number generator using the random number device. These are also part of the Standard C Library and available for use on iOS in Objective-C.

函数rand()和srand()是标准C库的一部分,与Objective-C在iOS开发中对我们完全可用的C库的其余部分一样。注意,这些例程已经被random()和srandom()所取代,它们几乎与rand()和srand()具有相同的调用约定,但是在更长的周期内产生更好的结果。还有一个srandomdev()例程,它使用随机数设备初始化随机数生成器的状态。这些也是标准C库的一部分,可用于Objective-C中的iOS。

#2


29  

Why don't you use arc4random which doesn't require a seed? You use it like this:

为什么不使用不需要种子的arc4random呢?你可以这样使用它:

int r = arc4random();

Here's an article comparing it to rand(). The arc4random() man page says this about it in comparison to rand():

这是一篇与rand()比较的文章。arc4random() man页面将其与rand()进行了比较:

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 (21700) states. The arc4random() function returns pseudo- random numbers in the range of 0 to (232)-1, and therefore has twice the range of rand(3) and random(3).

函数的作用是:使用arc4 cipher使用的密钥流生成器,使用8*8位s - box。s - box可以在21700个州。arc4random()函数在0到(232)-1的范围内返回伪随机数,因此它的范围是rand(3)和random(3)的两倍。

If you want a random number within a range, you can use the arc4random_uniform() function. For example, to generate a random number between 0 and 10, you would do this:

如果您想要一个范围内的随机数,可以使用arc4random_uniform()函数。例如,要生成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() 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)将返回一个比upper - bound小的均匀分布随机数。对于'' '' '' arc4random() % upper_bound'这样的结构,建议使用arc4random_uniform(),因为当上限不是2的幂时,它可以避免“模块偏差”。

#1


8  

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. 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. There is also an srandomdev() routine which initializes the state of the random number generator using the random number device. These are also part of the Standard C Library and available for use on iOS in Objective-C.

函数rand()和srand()是标准C库的一部分,与Objective-C在iOS开发中对我们完全可用的C库的其余部分一样。注意,这些例程已经被random()和srandom()所取代,它们几乎与rand()和srand()具有相同的调用约定,但是在更长的周期内产生更好的结果。还有一个srandomdev()例程,它使用随机数设备初始化随机数生成器的状态。这些也是标准C库的一部分,可用于Objective-C中的iOS。

#2


29  

Why don't you use arc4random which doesn't require a seed? You use it like this:

为什么不使用不需要种子的arc4random呢?你可以这样使用它:

int r = arc4random();

Here's an article comparing it to rand(). The arc4random() man page says this about it in comparison to rand():

这是一篇与rand()比较的文章。arc4random() man页面将其与rand()进行了比较:

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 (21700) states. The arc4random() function returns pseudo- random numbers in the range of 0 to (232)-1, and therefore has twice the range of rand(3) and random(3).

函数的作用是:使用arc4 cipher使用的密钥流生成器,使用8*8位s - box。s - box可以在21700个州。arc4random()函数在0到(232)-1的范围内返回伪随机数,因此它的范围是rand(3)和random(3)的两倍。

If you want a random number within a range, you can use the arc4random_uniform() function. For example, to generate a random number between 0 and 10, you would do this:

如果您想要一个范围内的随机数,可以使用arc4random_uniform()函数。例如,要生成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() 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)将返回一个比upper - bound小的均匀分布随机数。对于'' '' '' arc4random() % upper_bound'这样的结构,建议使用arc4random_uniform(),因为当上限不是2的幂时,它可以避免“模块偏差”。