3 回答
![?](http://img1.sycdn.imooc.com/533e4d00000171e602000200-100-100.jpg)
TA贡献1816条经验 获得超4个赞
我的解决方案是一个类别方法,该方法返回具有随机元素(使用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
- 3 回答
- 0 关注
- 538 浏览
添加回答
举报