打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
IOS随机产生字符串,数字

转:http://blog.sina.com.cn/s/blog_6ec3c9ce01016iil.html

产生随机数的几个函数:

            rand()、srand()、random()、arc4random()

使用 26 个英文字母产生10 个随机字母字符串

       int NUMBER_OF_CHARS =10;

       char data[NUMBER_OF_CHARS];

       for (intx=0;xchar)('A' +(arc4random_uniform(26))));

       NSString *dataPoint =[[NSString alloc]initWithBytes:data length:NUMBER_OF_CHARSencoding:NSUTF8StringEncoding];

2、产生随机数字

 

(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)));

          }

(4)、Getrandom value between 0 and 99

             int x = arc4random() % 100      Get random number between 500 and 1000
      int y =  (arc4random() % 501) + 500);
(5)、随机产生浮点数
      如果是一位小数:(float)(rand() + 10)/10.0f
      如果是两位小数:(float)(rand() + 10)/100.0f
           rand() 是算出0-9的数,+10后就是 10_19的数,除10后就是1.0-1.9之间的数了


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
ios 中生成随机数
[转载]ios随机数,and()、random()、arc4random()
mysql自动生成大量数据
Struts2实现的6位数字的验证码程序
SQL server 2000,Sybase及Oracle 随机取记录方法
iOS开发-生成随机数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服