`
blessdyb
  • 浏览: 231732 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Get random number in Objective-C by the function arc4random()

阅读更多

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));
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics