This paper briefly introduces several common methods of generating random numbers in objective-c, and shares them with us, hoping to bring some reference value to everyone. Specifically as follows:
1), Arc4random () method : More accurate does not need to generate the immediately seed
Use the following methods:
The code for obtaining integers between 0 and x-1 via Arc4random () is as follows:
int value = Arc4random ()% x;
The code to get an integer between 1 and X is as follows:
int value = (arc4random ()% x) + 1;
2), Ccrandom_0_1 () method : used in cocos2d , range is [0,1]
Use the following methods:
float random = ccrandom_0_1 () * 5; [0,5] ccrandom_0_1 () value range is [0,1]
3), Random () method : The seed should be set when initializing
Use the following methods:
Srandom ((unsigned int) time (time_t *) NULL); When initializing, set the seed.
The code in the article is available in a more detailed note, I believe it is not difficult to understand. Interested friends can test their own hands.