文章来自:http://blog.csdn.net/baidu_25743639/article/details/73801700
近期项目第三方登录之后默认创建账号和密码,就用随机数生产,这里只需要传入你需要几位的随机数即可,例如你需要得到8位的随机数,num就传入8
好了,废话少说,简单Demo如下:
- (NSString *)getRandomStringWithNum:(NSInteger)num { NSString *string = [[NSString alloc]init]; for (int i = 0; i < num; i++) { int number = arc4random() % 36; if (number < 10) { int figure = arc4random() % 10; NSString *tempString = [NSString stringWithFormat:@"%d", figure]; string = [string stringByAppendingString:tempString]; }else { int figure = (arc4random() % 26) + 97; char character = figure; NSString *tempString = [NSString stringWithFormat:@"%c", character]; string = [string stringByAppendingString:tempString]; } } return string; }