Cómo generar un número aleatorio no repetitivo.
Estoy tratando de aleatorizar números en una matriz. Soy capaz de hacer eso usandoarc4random() % [indexes count]
Mi problema es: si una matriz consta de 20 elementos, cada vez que la matriz se mezcla, en un lote de 5, debería aparecer un número diferente. Ejemplo:
primera baraja: 1,4,2,5,6.
segunda baraja: 7,12,9,15,3
-(IBAction)randomNumbers:(UIButton *)sender
{
int length = 10; // int length = [yourArray count];
NSMutableArray *indexes = [[NSMutableArray alloc] initWithCapacity:length];
for (int i=0; i<5; i++)
[indexes addObject:[NSNumber numberWithInt:i]];
NSMutableArray *shuffle = [[NSMutableArray alloc] initWithCapacity:length];
while ([indexes count])
{
int index = arc4random() % [indexes count];
[shuffle addObject:[indexes objectAtIndex:index]];
[indexes removeObjectAtIndex:index];
}
// for (int i=0; i<[shuffle count]; i++)
NSLog(@"%@", [shuffle description]);
}