洗牌 NSArray
Shuffle NSArray
我得到了一个 NSMutableArray,其中的对象数量很少,从 1 到最多 20 个不等。
当用户点击一个按钮时,我想 select 一个随机对象并显示它。我试过打乱数组:
srandom(time(NULL));
for (NSInteger x = 0; x < [array count]; x++) {
NSInteger randInt = (random() % ([array count] - x)) + x;
[array exchangeObjectAtIndex:x withObjectAtIndex:randInt];
}
然后 select 使用 arc4random() 的随机索引,但我重复了很多次。
我不想删除 ObjectAtIndex,因为这只会将我的数组减少到零然后崩溃。
有没有办法在达到零后用相同的对象重新填充数组并重新开始?
谢谢
如果您只需要随机选择一个对象,您可以使用以下方法。这将比实际对数组进行洗牌更有效。
uint32_t rnd = arc4random_uniform([array count]);
MyObject *randomObject = [array objectAtIndex:rnd];
归功于 this answer from Adam。
我通过执行以下操作"solve" 解决了我的问题:
它可能很乱,但我想就此发表一些意见。
NSUInteger randomIndex = arc4random()% [_someArray count];
if (array.count == 0) { //check if array is empty and add the randomIndex
[array addObject:@(randomIndex)];
randomTitle = [[_someArray objectAtIndex:randomIndex] myText];
// NSLog(@"array is empty");
// NSLog(@"my array content %@", array);
}
else if (array.count > 0 && array.count < _someArray.count) {//check if array has already an object and check if this is unique and add it to the array. if not run the loop again.
NSLog(@"array is betwen 0 and _someArray.count");
if([array indexOfObject:@(randomIndex)] == NSNotFound) {
[array addObject:@(randomIndex)];
randomTitle = [[_someArray objectAtIndex:randomIndex] myText];
// NSLog(@"unique number in array");
}
else {
[self loopBackTheAction];
// NSLog(@"number is alreday in array");
}
}
else if (array.count == _someArray.count) {//check if the array is full and empty it. run the loop again.
[array removeAllObjects];
[self loopBackTheAction];
// NSLog(@"array is full running again the loop");
}
我得到了一个 NSMutableArray,其中的对象数量很少,从 1 到最多 20 个不等。
当用户点击一个按钮时,我想 select 一个随机对象并显示它。我试过打乱数组:
srandom(time(NULL));
for (NSInteger x = 0; x < [array count]; x++) {
NSInteger randInt = (random() % ([array count] - x)) + x;
[array exchangeObjectAtIndex:x withObjectAtIndex:randInt];
}
然后 select 使用 arc4random() 的随机索引,但我重复了很多次。 我不想删除 ObjectAtIndex,因为这只会将我的数组减少到零然后崩溃。 有没有办法在达到零后用相同的对象重新填充数组并重新开始?
谢谢
如果您只需要随机选择一个对象,您可以使用以下方法。这将比实际对数组进行洗牌更有效。
uint32_t rnd = arc4random_uniform([array count]);
MyObject *randomObject = [array objectAtIndex:rnd];
归功于 this answer from Adam。
我通过执行以下操作"solve" 解决了我的问题: 它可能很乱,但我想就此发表一些意见。
NSUInteger randomIndex = arc4random()% [_someArray count];
if (array.count == 0) { //check if array is empty and add the randomIndex
[array addObject:@(randomIndex)];
randomTitle = [[_someArray objectAtIndex:randomIndex] myText];
// NSLog(@"array is empty");
// NSLog(@"my array content %@", array);
}
else if (array.count > 0 && array.count < _someArray.count) {//check if array has already an object and check if this is unique and add it to the array. if not run the loop again.
NSLog(@"array is betwen 0 and _someArray.count");
if([array indexOfObject:@(randomIndex)] == NSNotFound) {
[array addObject:@(randomIndex)];
randomTitle = [[_someArray objectAtIndex:randomIndex] myText];
// NSLog(@"unique number in array");
}
else {
[self loopBackTheAction];
// NSLog(@"number is alreday in array");
}
}
else if (array.count == _someArray.count) {//check if the array is full and empty it. run the loop again.
[array removeAllObjects];
[self loopBackTheAction];
// NSLog(@"array is full running again the loop");
}