随机排列

// .h

#import <Foundation/Foundation.h>

@interface NSMutableArray (VIMI)

- (void)shuffle;

@end

#import "NSMutableArray+VIMI.h"

@implementation NSMutableArray (VIMI)

- (void)shuffle

{

    NSUInteger count = [self count];

    for (uint i = 0; i < count; ++i)

    {

        // Select a random element between i and end of array to swap with.

        NSInteger nElements = count - i;

        NSInteger n = arc4random_uniform(nElements) + i;

        [self exchangeObjectAtIndex:i withObjectAtIndex:n];

    }

}

@end

原文地址:https://www.cnblogs.com/zhujin/p/4269968.html