Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number generating unique numbers in Objective-C?

Tags:

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.

like image 316
Tirth Avatar asked Jun 17 '11 11:06

Tirth


People also ask

What is arc4random_uniform?

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!


1 Answers

Check this links

  • Generating Random Numbers in Objective-C

  • 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 

  • Random Numbers in Objective-C
like image 185
Viktor Apoyan Avatar answered Sep 23 '22 02:09

Viktor Apoyan