iOS 随机数应用
使用arc4random()方法
//获取一个随机整数范围在:[0,100)包括0,不包括100
int x = arc4random() % 100;
1、获取数组随机值:
self.imageArray[(arc4random() %self.imageArray.count)];
NSArray *sta = @[@203,@205,@400,@401,@412,@402,@403,@406,@404,@408,@500,@410,@501,@-36,@-49,@-4,@-50,@-108,@-128];
NSInteger index = ((NSNumber*)sta[arc4random()%sta.count]).integerValue;
2、生成随机颜色:
[UIColor colorWithRed:(arc4random()%255)/255.0 green:(arc4random()%255)/255.0 blue:(arc4random()%255)/255.0 alpha:0.8];
3、 创建32位随机字符串
- (NSString *)createFileName {
char data[32];
for (int x=0;x<32;data[x++] = (char)('A' + (arc4random_uniform(26))));
return [[NSString alloc] initWithBytes:data length:32 encoding:NSUTF8StringEncoding];
}