ios

  • 获取一个随机整数范围在:[0,100)包括0,不包括100
// 方法1
int x = arc4random() % 100;
// 方法2
int x = arc4random_uniform(100);
  • 获取一个随机数范围在:[500,1000],包括500,包括1000
// 方法1
int y = (arc4random() % 501) + 500;
// 方法2
int y = arc4random_uniform(500 + 1) + 500;
  • 获取一个随机整数,范围在[from,to],包括from,包括to
// 范围在[from,to]
- (int)getRandomNumber:(int)from to:(int)to
{
    return arc4random_uniform(to - from + 1) + from;
//    return (arc4random() % (to - from + 1)) + from;
}