在这里,我使用以下代码对 NSMuttable 数组进行洗牌,而洗牌数组只重复一个值,其余的一切正常,我应该做哪些改变来洗牌而不重复?
words = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3",@"4", nil] ;
NSUInteger count = [words count];
for (int i = 0; i < count; i++)
{
NSInteger nElements = count - i;
NSInteger n = (arc4random() % nElements) + i;
NSLog(@"n val %d i val %d",i,n);
[words exchangeObjectAtIndex:i withObjectAtIndex:n];
}
NSlog 输出
2012-12-31 12:49:05.730 quizer[1607:c07] n val 0 i val 0
2012-12-31 12:49:05.731 quizer[1607:c07] n val 1 i val 3
2012-12-31 12:49:05.731 quizer[1607:c07] n val 2 i val 2
2012-12-31 12:49:05.732 quizer[1607:c07] n val 3 i val 3
请帮我解决