简体   繁体   English

如何在Objective-C中播种rand()函数?

[英]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.

相关问题 如何播种随机生成器并在Objective-C中创建随机int - How do I seed the random generator and create a random int in Objective-C 我该如何回调此Objective-C函数的结果? - How do I do a callback of the result of this objective-c function? 如何在Objective-C中在运行时创建函数 - How do I create a function at runtime in Objective-C 如何在Xcode中找到Objective-C函数的用法? - How do I find usages of an Objective-C function in Xcode? Objective-C 中 rand() 之后的 if 语句 - If statement after rand() in Objective-C 如何在Objective-c中为C函数指定一个指向'self'(调用obj)的指针? - How do I give C function a pointer to 'self' (calling obj) in objective-c? 如何将普通ole C函数设为Objective-C选择器? - How do I make a plain ole C-function an Objective-C selector? Objective-C属性如何与C ++对象一起运行 - How do objective-C properties function with C++ objects 我如何在不使用sleep()的情况下暂停Mac上c或Objective-c中的函数 - How do I pause a function in c or objective-c on mac without using sleep() 如何使C函数和预处理器宏可用于几个Objective-C类? - How do I make a C function and preprocessor macro available to several Objective-C classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM