iOS开发-生成随机数


有时候我们需要在程序中生成随机数,但是在Objective-c中并没有提供相应的函数,好在C中提供了rand()srand()random()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)));

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM