第一种方法:笨方法
- -(NSMutableArray*)getRandomArrFrome:(NSArray*)arr
- {
- NSMutableArray *newArr = [NSMutableArray new];
- while (newArr.count != arr.count) {
- //生成随机数
- int x =arc4random() % arr.count;
- id obj = arr[x];
- if (![newArr containsObject:obj]) {
- [newArr addObject:obj];
- }
- }
- return newArr;
- }
第二种方法:苹果官方方法
- NSArray* arr = @[@"1",@"2",@"3"];
- arr = [arr sortedArrayUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2) {
- int seed = arc4random_uniform(2);
- if (seed) {
- return [str1 compare:str2];
- } else {
- return [str2 compare:str1];
- }
- }];