是否有一种规范的方法可以在Objective C中随机化一个数组?
答案 0 :(得分:59)
我的实用程序库在NSMutableArray上定义了这个类别:
@interface NSMutableArray (ArchUtils_Shuffle)
- (void)shuffle;
@end
// Chooses a random integer below n without bias.
// Computes m, a power of two slightly above n, and takes random() modulo m,
// then throws away the random number if it's between n and m.
// (More naive techniques, like taking random() modulo n, introduce a bias
// towards smaller numbers in the range.)
static NSUInteger random_below(NSUInteger n) {
NSUInteger m = 1;
// Compute smallest power of two greater than n.
// There's probably a faster solution than this loop, but bit-twiddling
// isn't my specialty.
do {
m <<= 1;
} while(m < n);
NSUInteger ret;
do {
ret = random() % m;
} while(ret >= n);
return ret;
}
@implementation NSMutableArray (ArchUtils_Shuffle)
- (void)shuffle {
// http://en.wikipedia.org/wiki/Knuth_shuffle
for(NSUInteger i = [self count]; i > 1; i--) {
NSUInteger j = random_below(i);
[self exchangeObjectAtIndex:i-1 withObjectAtIndex:j];
}
}
@end
确保在调用之前的某个时间为随机数生成器(例如srandom(time(NULL))
)播种;否则输出不会很随机。
答案 1 :(得分:20)
这是!
- (NSArray*)shuffleArray:(NSArray*)array {
NSMutableArray *temp = [[NSMutableArray alloc] initWithArray:array];
for(NSUInteger i = [array count]; i > 1; i--) {
NSUInteger j = arc4random_uniform(i);
[temp exchangeObjectAtIndex:i-1 withObjectAtIndex:j];
}
return [NSArray arrayWithArray:temp];
}
答案 2 :(得分:7)
if ([array count] > 1) {
for (NSUInteger shuffleIndex = [array count] - 1; shuffleIndex > 0; shuffleIndex--)
[array exchangeObjectAtIndex:shuffleIndex withObjectAtIndex:random() % (shuffleIndex + 1)];
}
确保使用srandomdev()或srandom()为random()函数设定种子。
答案 3 :(得分:2)
如果您要问的话,SDK中没有内置功能。</ p>
您可以使用任何您想要的随机化或随机算法。不同的算法在随机性,效率等方面有不同的权衡。
http://en.wikipedia.org/wiki/Shuffling#Shuffling_algorithms
对于使用可变数组启动“就地”的算法,请使用
insertObject:atIndex:
removeObjectAtIndex:
对于重建数组的算法,将其提供给原始数据并构建一个新数组。
答案 4 :(得分:1)
我的解决方案是一个类别方法,它返回一个数组的副本(自动释放),其元素随机化(使用arc4random)。
@interface NSArray (CMRandomised)
/* Returns a copy of the array with elements re-ordered randomly */
- (NSArray *)randomised;
@end
/* Returns a random integer number between low and high inclusive */
static inline int randomInt(int low, int high)
{
return (arc4random() % (high-low+1)) + low;
}
@implementation NSArray (CMRandomised)
- (NSArray *)randomised
{
NSMutableArray *randomised = [NSMutableArray arrayWithCapacity:[self count]];
for (id object in self) {
NSUInteger index = randomInt(0, [randomised count]);
[randomised insertObject:object atIndex:index];
}
return randomised;
}
@end
答案 5 :(得分:0)
如果没有NSArray
上的类别(即有arrayWithRandomizedIndices
)或NSMutableArray
之类的实例方法,则没有规范方法(即有类似randomizeIndices
的方法)。
以下是我的图书馆中的一个示例,它是NSMutableArray
上某个类别的一部分。它将随机重新排序数组,而不是随机播放几个条目。
- (void) randomizeIndices
{
if (self == nil || [self count] <= 1)
{
return;
}
int count = [self count];
NSMutableArray* copySelf = [NSMutableArray arrayWithArray:self];
NSMutableArray* mutableResultArray = [NSMutableArray alloc];
mutableResultArray = [mutableResultArray initWithCapacity:count];
[mutableResultArray autorelease];
int objectsMovedCount = 0;
for (int i = 0; i < count; i++)
{
int index = rand() % (count - objectsMovedCount);
id anObject = [copySelf objectAtIndex:index];
[mutableResultArray addObject:anObject];
[copySelf removeObjectAtIndex:index];
objectsMovedCount++;
}
[self setArray:mutableResultArray];
}
在调用此方法之前或方法的早期调用srand(time(0));
或其他类似内容。
答案 6 :(得分:0)
NSArray随机化为Objective-C类别方法:
@implementation NSArray (NGDataDynamics)
- (NSArray *)jumbled
{
NSMutableArray *jumbled = self.mutableCopy;
NSUInteger idx = self.count-1;
while(idx)
{
[jumbled exchangeObjectAtIndex:idx
withObjectAtIndex:arc4random_uniform(idx)];
idx--;
}
return jumbled;
}
@end