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

 

你可能感兴趣的:(C++,c,C#,Objective-C)