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.
-
Check out the many other questions on this topic, e.g. http://stackoverflow.com/questions/160890/generating-random-numbers-in-objective-c – pmdj Jun 17 '11 at 11:28
-
7use arc4random() %(100); – iAmitWagh Jun 17 '11 at 11:28
-
3I came across this, and I thought I should share that you should prefer arc4random_uniform(X) to arc4random() % X – borrrden Jul 27 '12 at 04:35
6 Answers
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

- 5,118
- 5
- 41
- 54

- 10,655
- 22
- 85
- 147
arc4random() %(100)-1
this is worked for me.

- 9,750
- 1
- 60
- 91

- 7,801
- 9
- 55
- 88
-
3From `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
Use arc4random and store the results in an NSSet which will take care of the duplicates for you.

- 146,289
- 39
- 203
- 257
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

- 605
- 1
- 5
- 11
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..
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);
}

- 385
- 15
- 21