随机排列

// .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

 

posted on 2015-02-03 14:44  助金  阅读(136)  评论(0编辑  收藏  举报