Como gerar um número aleatório não repetitivo
Eu estou tentando randomizar números em uma matriz. Eu sou capaz de fazer isso usandoarc4random() % [indexes count]
Meu problema é - Se um array é composto por 20 itens, toda vez que o array embaralha, em um lote de 5, números diferentes devem aparecer. Exemplo:
primeiro shuffle: 1,4,2,5,6.
segundo shuffle: 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]);
}