码迷,mamicode.com
首页 > 其他好文 > 详细

随机排列

时间:2015-02-03 16:42:46      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

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

 

随机排列

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!