1

Possible Duplicate:
Non repeating random numbers in Objective-C

How to generate non repeating random numbers?

I saw this on many sites but they give in main.c file code.

When I use the main.c file the code working is fine, but when I try to convert in to my.m file it is not working.

example:

I need to get all the numbers between 0-10 randomly.and the numbers should not repeat again.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Thukaram
  • 1,085
  • 2
  • 13
  • 33
  • 2
    can you supply the code that's not working? when you say it's not working, what error/issue are you hitting? – bryanmac Dec 07 '11 at 04:57

6 Answers6

2

Hope the below lines of code will help you

srand(time(NULL));
int randomIndex = rand() % limit;
[reportIDTextField setText:[NSString stringWithFormat:@"%i", randomIndex]];

Benefit of this code is it won't allow repetitive random numbers. Here limit for random numbers.

Hope this is what you are looking for.

Enjoy Coding :)

Mrunal
  • 13,982
  • 6
  • 52
  • 96
2
    NSMutableArray *storeArray = [[NSMutableArray alloc] init];    
    BOOL record = NO;
    int x;

    for (int i=0; [storeArray count] < 10; i++) //Loop for generate different random values
    {
        x = arc4random() % 10;//generating random number
        if(i==0)//for first time 
        {
          [storeArray addObject:[NSNumber numberWithInt:x]];  
        }
        else
        {
            for (int j=0; j<= [storeArray count]-1; j++) 
            {
                    if (x ==[[storeArray objectAtIndex:j] intValue]) 
                    record = YES;
            }

            if (record == YES) 
            {
                record = NO;
            }
            else
            {
                [storeArray addObject:[NSNumber numberWithInt:x]];
            }
        }
    }
    NSLog(@" Non Repeated Random Numbers : %@",storeArray);

can u try this code may be it's use full to you

Ilan
  • 401
  • 1
  • 8
  • 21
Thukaram
  • 1,085
  • 2
  • 13
  • 33
  • 3
    This is an extremely inefficient algorithm. – omz Dec 13 '11 at 07:53
  • the checking part should be:if (x ==[[storeArray objectAtIndex:j] intValue]) record = YES; break; } because without BREAK the loop will check all the array even if the repetition has been already found.... – Ilan Aug 03 '13 at 20:56
2
srand(time(NULL));
 int s[10];
 BOOL fl = 1;
for (int i = 0; i<10; i++) {
   while (fl) {
      s[i] = rand()%10;
      fl = 0;
      for (int j = 0; j < i; j++) {
         if (s[j] == s[i]) {fl = 1; j = i+1;}
      }
   }
}
SentineL
  • 4,682
  • 5
  • 19
  • 38
2
int n = 10;
NSMutableArray *numbers = [NSMutableArray array];
for (int i = 0; i <= n; i++) {
    [numbers addObject:[NSNumber numberWithInt:i]];
}
NSMutableArray *result = [NSMutableArray array];
while ([numbers count] > 0) {
    int r = arc4random() % [numbers count];
    NSNumber *randomElement = [numbers objectAtIndex:r];
    [result addObject:randomElement];
    [numbers removeObjectAtIndex:r];
}
NSLog(@"%@", result);
omz
  • 53,243
  • 5
  • 129
  • 141
0

Try this:- This will produce numbers between 0 to 10 randomly.

 srand(time(NULL));
int n = 11;
int card[n];  
for (int y=0; y<n; y++) {
    card[y] = y;  
}
for(int q= 0 ;q<3;q++) { 
    for (int y=0; y<(n-1); y++) {
        int r = y + (arc4random() % (n-y));         
        int temp = card[y]; card[y] = card[r]; card[r] = temp;
    }
}
for (int g=0;g<n ; g++) {
    int w = card[g];
    NSLog(@"%i",w);
}
Leena
  • 2,678
  • 1
  • 30
  • 43
0

You can do something like this

number = (arc4random()%10)+1; //Generates Number from 1 to 10.

save it to an Array Then you can put a condition,That will check the number whether it is present or not in the array. if not then add it to the array ,if it is already present then simply discard it.

A NOTE: "Never, ever add or multiply random numbers in an attempt to get 'better' randomness :)

Ajeet Pratap Maurya
  • 4,244
  • 3
  • 28
  • 46