+ (NSArray *)getRandomWithPosition:(NSInteger)position positionContent:(id)positionContent array:(NSArray *)baseArray {
NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:baseArray.count];
NSMutableArray *tempBaseArray = [NSMutableArray arrayWithArray:baseArray];
while ([tempBaseArray count]) {
NSInteger range = [tempBaseArray count];
id string = [tempBaseArray objectAtIndex:arc4random()%range];
[resultArray addObject:string];
[tempBaseArray removeObject:string];
}
// 计算指定数组元素在新数组中的位置
NSUInteger index = [resultArray indexOfObject:positionContent];
// 交换数组元素
[resultArray exchangeObjectAtIndex:index withObjectAtIndex:position];
return resultArray;
}
根据一个整齐的数据,随机的得到一个新的数组,可指定数组元素出现的位置
原文:http://www.cnblogs.com/joesen/p/4364253.html