//将一个数组元素顺序打乱
+ (NSMutableArray *)gibberishArray:(NSArray *)array{
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:array.count];
NSMutableArray *tempArr = [[NSMutableArray alloc] initWithArray:array];
int count = (int)array.count;
int randCount = 0;//索引
int nowCount = 0;//当前数组的元素个数
for (;count != 0; ) {
nowCount = (int)tempArr.count;//当前数组的元素个数
count = nowCount;
if (nowCount != 0) {
randCount = (arc4random() % nowCount);
[arr addObject:tempArr[randCount]];
tempArr[randCount] = tempArr.lastObject;
[tempArr removeLastObject];
}
}
return arr;
}
网友评论