1

Possible Duplicate:
Generating non-repeating random numbers

Here is my code

NSUInteger count = 10;
for (NSUInteger i = 0; i < count; ++i) {
    NSLog(@"%d",NeedRandomNumberWithoutRepeat);
}

this output should be like 8 7 9 2 1 4 6 3 5 0 Which is random and not repeating numbers

Community
  • 1
  • 1
SachinVsSachin
  • 6,401
  • 3
  • 33
  • 39
  • What you want? id total numbers are 10, you need to generate random number 1 to 10, only once? What for 11th time? – Anoop Vaidya Dec 14 '12 at 13:42
  • not problem with generate random number, Problem with repeating number – SachinVsSachin Dec 14 '12 at 13:44
  • You probably want a "random permutation" of N numbers. http://stackoverflow.com/questions/8554292/generating-non-repeating-random-numbers contains links to algorithms. – Martin R Dec 14 '12 at 13:53

3 Answers3

1

This should work:

NSUInteger count = 10;

NSMutableArray *array = [[NSMutableArray alloc]init];
for (NSUInteger i = 0; i < count; ++i) {
    [array addObject:[NSNumber numberWithInt:i]];
}

NSMutableArray *copy = [array mutableCopy];
array = [[NSMutableArray alloc]init];
while ([copy count] > 0)
{
    int index = arc4random() % [copy count];
    id objectToMove = [copy objectAtIndex:index];
    [array addObject:objectToMove];
    [copy removeObjectAtIndex:index];
}
Roland Keesom
  • 8,180
  • 5
  • 45
  • 52
0

This answer is modified version from one of my answer in SO.

So, you may find something strange here, you can however use this as your requirement is similar.

int TOTAL_NUMBER=10;
NSMutableArray *alreadyGeneratedNumbers;


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
    alreadyGeneratedNumbers=[NSMutableArray new];
}

-(int)generateRandomNumber{
    int low_bound = 0;
    int high_bound = TOTAL_NUMBER;
    int width = high_bound - low_bound;
    int randomNumber = low_bound + arc4random() % width;

    return randomNumber;
}

- (IBAction)button:(id)sender {
    NSMutableArray *shuffle = [[NSMutableArray alloc] initWithCapacity:1];

    BOOL contains=YES;
    while ([shuffle count]<1) {
        NSNumber *generatedNumber=[NSNumber numberWithInt:[self generateRandomNumber]];

        if (![alreadyGeneratedNumbers containsObject:generatedNumber]) {
            [shuffle addObject:generatedNumber];
            contains=NO;
            [alreadyGeneratedNumbers addObject:generatedNumber];
        }
    }

    NSLog(@"shuffle %@",shuffle);
    NSLog(@"Next Batch");

    if ([alreadyGeneratedNumbers count] >= TOTAL_NUMBER) {
        NSLog(@"\nGame over, Want to play once again?");//or similar kind of thing.
        [alreadyGeneratedNumbers removeAllObjects];
    }
}
Community
  • 1
  • 1
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
-1

You put the available numbers in an array, and take the index calculated with arc4random() that goes from 0 to the size of the array -1.When a number comes out you take it away from the array:

NSMutableArray* availableNumbers=[NSMutableArray new];
for(NSUInteger i=0; i<10; i++)
{
    [availableNumbers addObject: @(i)];
}
for(NSUInteger i=0; i<10; i++)
{
    NSUInteger index= arc4random()%availableNumbers.count;
    NSNumber* number= availableNumbers[index];
    NSLog(@"%@",number);
    [availableNumbers removeObjectAtIndex: index];
}

PS: At the last iteration is useless to sue arc4random(), since there's only one number inside.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187