银行支付控件之 自动随机(shuffle)密码键盘的实现算法

算法一
NSMutableArray *randSequence = [[NSMutableArray alloc] initWithCapacity:8];
for (int ii = 0; ii < 10; ++ii)
[randSequence addObject:[NSNumber numberWithInt:ii]];

for (int ii = 9; ii > -1; --ii) {
int r = arc4random() % 9
[randSequence exchangeObjectAtIndex:ii withObjectAtIndex:r];
}

算法二
@interface NSMutableArray (Shuffling)

  • (void)shuffle;
    @end

// NSMutableArray_Shuffling.m

import "NSMutableArray_Shuffling.h"

@implementation NSMutableArray (Shuffling)

  • (void)shuffle
    {
    NSUInteger count = [self count];
    for (NSUInteger 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() % nElements) + i;
    [self exchangeObjectAtIndex:i withObjectAtIndex:n];
    }
    }

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容