Three random number methods in iOS and three random numbers in ios
Ios has the following three random number methods:
// The first srand (unsigned) time (0); // the random number generated each time without adding this sentence does not change int I = rand () % 5; // srandom (time (0); int I = random () % 5; // The third int I = arc4random () % 5;
Note:
①Rand ()AndRandom ()Actually, it is not a real pseudo-random number generator.InitializationRandom Seed. Otherwise, the random number generated each time is the same.
②Arc4random ()It is a real pseudo-random algorithm and does not need to generate random seeds, because it is automatically generated during the first call. And the range is twice that of rand. In the iPhone, RAND_MAX is 0x7fffffff (2147483647), while the maximum value returned by arc4random () is 0x100000000 (4294967296 ).
PrecisionComparison: arc4random ()> random ()> rand ()
Appendix: set of common arc4random () Methods
// Obtain a random integer in the range of [0,100) 0, excluding 100int x = arc4random () % 100; // obtain a random number in the range ), including 500, excluding 1000int y = (arc4random () % 501) + 500; // obtain a random integer in the range of [from, to), including from, not including to-(int) getRandomNumber :( int) from to :( int) to {return (int) (from + (arc4random () % (to-from + 1 )));}