配列内の数値をランダム化しようとしています。私はそれを使用してそれを行うことができますarc4random() % [indexes count]
私の問題は-配列が20個のアイテムで構成されている場合、配列がシャッフルされるたびに、5つのバッチで異なる数が表示されるはずです。例 :
最初のシャッフル:1、4、2、5、6。
2番目のシャッフル: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]);
}