22

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tirth
  • 7,801
  • 9
  • 55
  • 88

6 Answers6

78

Check this links



  • 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
    

clozach
  • 5,118
  • 5
  • 41
  • 54
Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147
6

arc4random() %(100)-1 this is worked for me.

Suresh Varma
  • 9,750
  • 1
  • 60
  • 91
Tirth
  • 7,801
  • 9
  • 55
  • 88
  • 3
    From `man arc4random`: *arc4random_uniform() will return a uniformly distributed random number less than upper_bound. arc4random_uniform() is recommended over constructions like ``arc4random() % upper_bound'' as it avoids "modulo bias" when the upper bound is not a power of two.* – Manav Sep 11 '12 at 07:45
  • also, note that the upper bound in `u_int32_t arc4random_uniform(u_int32_t upper_bound)` is not inclusive, i.e. `arc4random_uniform(1)` will always give you zeroes. – Manav Sep 11 '12 at 07:58
5

Use arc4random and store the results in an NSSet which will take care of the duplicates for you.

Abizern
  • 146,289
  • 39
  • 203
  • 257
4

An other easy way is to do this is the use the arc4random_uniform() method:

Calling arc4random_uniform(N) return a random number from 0 to N-1. To get a number from 1 to N, you can use:

NSUInteger r = arc4random_uniform(N) + 1;

where N is the max number that you are looking for. In Swift, you can do it as:

var randomIndex:UInt32 = arc4random_uniform(N) + 1

e.g. if you want to get a random number between 1 and 100, just call as:

NSUInteger r = arc4random_uniform(100) + 1;

or in swift:

var randomIndex:UInt32 = arc4random_uniform(100)+1
momentsnapper
  • 605
  • 1
  • 5
  • 11
2

This is the code which generate unique random numbers...

-(void)UniqueRandom{

    int T[11];
    BOOL flag;
    for(int i=0;i<10;i++){

        int ranNo=  random()%100+1;
        flag=TRUE;
        int s=(sizeof T); 

        for(int x=0;x<s;x++){

            if(ranNo==T[x]){
                i--;
                flag= FALSE;
                break;
            }
        }

        if(flag) T[i]=ranNo;
    }

    for(int j=0;j<100;j++)  NSLog(@"unique random %d",T[j]);
    }
}

Happy coding..

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Asish AP
  • 4,421
  • 2
  • 28
  • 50
1

This method will generate array of unique random number in the interval of high and low range.

-(void)generateRandomUniqueNumberInRange :(int)rangeLow :(int)rangeHigh{
    NSMutableArray *unqArray=[[NSMutableArray alloc] init];
    int randNum = arc4random() % (rangeHigh-rangeLow+1) + rangeLow;
    int counter=0;
    while (counter<rangeHigh-rangeLow) {
        if (![unqArray containsObject:[NSNumber numberWithInt:randNum]]) {
            [unqArray addObject:[NSNumber numberWithInt:randNum]];
            counter++;
        }else{
            randNum = arc4random() % (rangeHigh-rangeLow+1) + rangeLow;
        }

    }
    NSLog(@"UNIQUE ARRAY %@",unqArray);

}
iSankha007
  • 385
  • 15
  • 21