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

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

作者: seventhboy | 来源:发表于2017-02-06 14:46 被阅读61次

算法一
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

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/eqfuittx.html