iOs下生成随机数的最简的单方法

来源:互联网 发布:香港知识产权软件 编辑:程序博客网 时间:2024/06/06 10:52

f you need to generate a random number within your iPhone application, you have to push aside Objective-C, as there is not a class with a method for generating random numbers (as in Java). The alternative is to use C, among the functions available are: rand()srand()random(),srandom() and arc4random().

arc4random() tends to be preferred as it does not require seeding, the function automatically initializes itself.

// Get random value between 0 and 99int x = arc4random() % 100// Get random number between 500 and 1000int y =  (arc4random() % 501) + 500);