OC
//随机字母
- (NSString *)randomStringWithNumber:(NSInteger)number{ //number 是需要的个数
NSString *ramdom;
NSMutableArray *array = [NSMutableArray array];
for (int i = 1; i ; i ++) {
int a = (arc4random() % 122); //如需要可以改变数值大小 这儿的数值是ASCII值
if (a > 96) { //这儿是小写字母 如需要自行更改
char c = (char)a;
[array addObject:[NSString stringWithFormat:@"%c",c]];
if (array.count == number) {
break;
}
} else continue;
}
ramdom = [array componentsJoinedByString:@""];//这个是把数组转换为字符串
return ramdom;
}
swift
/** 随机产生字符串
parameters:
- number 字符串长度
- digital 是否包含数字
- capital 是否包含大写
- lowercase 是否包含小写
*/
class func getRandomString(_ number:Int! ,_ digital:Bool!,_ capital:Bool!,_ lowercase:Bool!) -> String{
var number:Int! = number
if number < 1 {
number = 32
}
var strArray:Array<String> = Array()
for _ in 0...INT64_MAX {
let a:Int = Int(arc4random() % 122)
let c:Character = Character(UnicodeScalar(a)!)
//包含数字
if digital {
if a > 47 && a < 58{
strArray.append(String.init(c))
}
}
//大写字母
if capital {
if a > 64 && a < 92 {
strArray.append(String.init(c))
}
}
//小写字母
if lowercase {
if a > 96 && a < 123 {
strArray.append(String.init(c))
}
}
if strArray.count == number{
break
}
}
let str:String = strArray.joined(separator: "")
return str;
}
自己照图更改数值(有些我不确定能行哦!)