I want to generate random numbers between 1-100 in Objective-C. Each random number should be unique and not the same as a previously generated random number.
As you know the function arc4random_uniform(_:) returns an integer between zero and the upper bound. If we use array. count as the upper bound, the function will return an index number within the bounds of the array!
Check this links
How do I generate random numbers on the iPhone?
int r = arc4random() % 100;
Objective-C: Get random number
-(int)getRandomNumberBetween:(int)from and:(int)to { return (int)from + arc4random() % (to-from+1); }
How to use:
1) Implement method above into your .m file
2) Add the following line to your .h file:
-(int)getRandomNumberBetween:(int)from and:(int)to;
3) Call the method like:
int randomNumber = [self getRandomNumberBetween:9 and:99]; //this gets you a random number between 9 and 99
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With