ios 中生成随机数
来源:互联网 发布:java 数据字典设计 编辑:程序博客网 时间:2024/05/22 12:20
ios 中生成随机数
ios 有如下三种随机数方法:
1. srand((unsigned)time(0)); //不加这句每次产生的随机数不变
int i = rand() % 5;
2. srandom(time(0));
int i = random() % 5;
3. int i = arc4random() % 5 ;
注:rand()和random()实际并不是一个真正的伪随机数发生器,在使用之前需要先初始化随机种子,否则每次生成的随机数一样。
arc4random() 是一个真正的伪随机算法,不需要生成随机种子,因为第一次调用的时候就会自动生成。而且范围是rand()的两倍。在iPhone中,RAND_MAX是0x7fffffff (2147483647),而arc4random()返回的最大值则是 0x100000000 (4294967296)。
精确度比较:arc4random() > random() > rand()。
常用方法:arc4random
1、获取一个随机整数范围在:[0,100)包括0,不包括100
int x = arc4random() % 100;
2、 获取一个随机数范围在:[500,1000),包括500,不包括1000
int y = (arc4random() % 501) + 500;
3、获取一个随机整数,范围在[from,to),包括from,不包括to
-(int)getRandomNumber:(int)from to:(int)to
{
return (int)(from + (arc4random() % (to – from + 1)));
}
参考:
1.http://kb.cnblogs.com/a/2335710/
2.http://mmz06.blog.163.com/blog/static/121416962011101103734767/
- ios 中生成随机数
- ios 中生成随机数
- ios 中生成随机数
- ios 中生成随机数
- ios 中生成随机数
- ios 中生成随机数
- ios 中生成随机数
- ios 中生成随机数
- ios 中生成随机数
- ios 中生成随机数
- ios中生成随机数
- iOS中生成随机数
- iOS中生成随机数arc4random
- iOS中arc4random生成随机数
- iOS开发中生成随机数
- ios生成随机数
- iOS生成随机数
- 【IOS基础知识】生成随机数
- toj3095 Best Compression Ever 不懂 求解释?
- Solaris 使用ndd指令设置网卡速率
- 习题2-7 近似计算
- C/C++/JAVA/语法大PK——访问控制
- 从solaris explorer鉴别DISK 硬件故障识别方法
- ios 中生成随机数
- What is a "sosreport" and how do I create it in Red Hat Enterprise Linux 4.6 and later
- 基于移动终端(平板电脑、手机)的网络调查
- struct sizeof
- How to interpret the meanings of HDD LEDs
- 图像分割方法
- 如何快速方便的输出向量vector容器中不重复的内容
- SUN X4200 Server, All Leds of HDD0 is not lit
- weblogic 部署应用时一直停留在部署状态解决办法