I would like to have an array with 12 numbers -> 0 to 11 the array must be random, and i don't want to have twice the same number
thanx
I would like to have an array with 12 numbers -> 0 to 11 the array must be random, and i don't want to have twice the same number
thanx
Sounds like a shuffling problem.
Just declare an array like follows
NSMutableArray * numbers = [NSMutableArray array];
for (int i = 0; i < 12; i++) {
[numbers addObject:@i];
}
Then you can shuffle that array using the Fisher-Yates algorithm
for (NSUInteger i = numbers.count - 1; i > 0; --i) {
NSUInteger n = arc4random_uniform(i+1);
[numbers exchangeObjectAtIndex:i withObjectAtIndex:n];
}
I suggest you create an array and fill it in a loop with the numbers 0 to 11. In a second step, you shuffle that array: What's the Best Way to Shuffle an NSMutableArray?
You could try something like:
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0; i < 12; i++) {
int randomNumber = min + rand() % (max-min);
[array addObject:[NSNumber numberWithInt:randomNumber]];
}
Not sure if the syntax is correct, im on a Windows machine now