1、随机生成数
//1.整型随机生成 int x = arc4random() % (to - from + 1) + from;//[from,to]范围 int number = arc4random() % (3 - 2 + 1) + 2;//[2,3] //2.浮点数随机生成 #define ARC4RANDOM_MAX 0x100000000 //然后就可以使用arc4random() 来获取0到100之间浮点数了(精度是rand()的两倍),代码如下: double val = floorf(((double)arc4random() / ARC4RANDOM_MAX) * 100.0f);
2、字符串随机生成
-(NSString *)randomStringWithLength:(NSInteger)len { NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; NSMutableString *randomString = [NSMutableString stringWithCapacity: len]; for (NSInteger i = 0; i < len; i++) { [randomString appendFormat: @"%C", [letters characterAtIndex: arc4random_uniform([letters length])]]; } return randomString; }