首页 > 代码库 > 【代码笔记】iOS-产生随机字符串

【代码笔记】iOS-产生随机字符串

一,代码:

技术分享
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSLog(@"---产生随机字符串---%@",[self generateTradeNO]);
    
}
//产生随机字符串
- (NSString *)generateTradeNO
{
    static int kNumber = 15;
    
    NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRST";
    NSMutableString *resultStr = [[NSMutableString alloc] init];
    srand(time(0));
    for (int i = 0; i < kNumber; i++)
    {
        unsigned index = rand() % [sourceStr length];
        NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
        [resultStr appendString:oneStr];
    }
    return resultStr;
}
技术分享

 

二,输出:

2015-10-23 10:04:22.908 产生随机字符串[974:46634] ---产生随机字符串---DQA5AGLNIBD52KQ

 

 

【代码笔记】iOS-产生随机字符串