Swift - 使用arc4random()、arc4random_uniform()取得随机数

arc4random() 这个全局函数会生成10位数的随机整数(UInt32)。其生成的最大值是4294967295(2^32 - 1),最小值为0。// @alphaZ

1、下面是使用 arc4random 函数求一个 1~100 的随机数(包括1和100)

  let temp = Int(arc4random()%100)+1

2、下面是使用 arc4random_uniform 函数求一个 1~100 的随机数(包括1和100)// @alphaZ

let temp = Int(arc4random_uniform(100))+1  

原文出自:www.hangge.com 转载请保留原文链接:http://www.hangge.com/blog/cache/detail_512.html

你可能感兴趣的:(Swift - 使用arc4random()、arc4random_uniform()取得随机数)