The C language offers a few choices for random number generation. I chose the arc4random() function. This more modern variant of the traditional rand() function is automatically seeded so that it does not reproduce the same sequence of random numbers each time an app launches. The function returns a signed integer between −2,147,483,648 and +2,147,483,648. If you want a random integer between
zero and some positive integer, use the modulus operator (%).
For example, the following produces random positive integers between 0 and 9 (inclusive):
int oneRandomInt = arc4random() % 10;
Or, to obtain a positive random integer across the entire range of values, use the following variant:
int oneRandomInt = (arc4random() % ((unsigned)RAND_MAX + 1));
本文介绍了C语言中现代的随机数生成方法,重点讲解了arc4random()函数的使用方式及如何生成特定范围内的随机整数。
1017

被折叠的 条评论
为什么被折叠?



