mercredi 4 mars 2015

Objective-C always generates same random number

I am developing an application in Objective-C. i have to create random number in range.


I executed the below code two times consecutively and i clicked 6 time to random number generator button.



int RandNum = rand() % (10000 - 100)+100;
int RandNum2 = rand() % (10000 - 200)+100;
int RandNum3 = rand() % (10000 - 300)+100;
NSLog(@"%i - %i - %i",RandNum,RandNum2,RandNum3);


1st output :



2015-03-04 10:32:08.700 MyApp[2538:60b] 7007 - 149 - 5073
2015-03-04 10:32:10.806 MyApp[2538:60b] 2658 - 8030 - 3872
2015-03-04 10:32:11.174 MyApp[2538:60b] 8044 - 2978 - 4723
2015-03-04 10:32:11.685 MyApp[2538:60b] 2909 - 1940 - 6165
2015-03-04 10:32:11.990 MyApp[2538:60b] 9592 - 8142 - 8587
2015-03-04 10:32:12.294 MyApp[2538:60b] 2703 - 6227 - 3029


2nd output :



2015-03-04 10:32:52.103 MyApp[2543:60b] 7007 - 149 - 5073
2015-03-04 10:32:52.807 MyApp[2543:60b] 2658 - 8030 - 3872
2015-03-04 10:32:53.336 MyApp[2543:60b] 8044 - 2978 - 4723
2015-03-04 10:32:53.862 MyApp[2543:60b] 2909 - 1940 - 6165
2015-03-04 10:32:54.359 MyApp[2543:60b] 9592 - 8142 - 8587
2015-03-04 10:32:55.223 MyApp[2543:60b] 2703 - 6227 - 3029


So, it's always creating same number. But when i edit the code rand() to arc4random() the result is changed. and the its creating the random numbers.


Now, I want to ask why rand() is not working?





Aucun commentaire:

Enregistrer un commentaire