整型隨機數(shù)
如果我們想要一個整型的隨機數(shù)贝搁,則可以考慮用arc4random系列函數(shù)盏缤。我們可以通過man arc4random命令來看一下這個函數(shù)的定義:
The arc4random() function uses the key stream generator employed by the arc4 cipher, which uses 8*8 8 bit S-Boxes. The S-Boxes can be inabout (21700) states. The arc4random() function returns pseudo-random numbers in the range of 0 to (232)-1, and therefore has twice the range of rand(3) and random(3).
arc4random使用了arc4密碼加密的key stream生成器(請腦補)靴患,產(chǎn)生一個[0, 2^32)區(qū)間的隨機數(shù)(注意是左閉右開區(qū)間)祭陷。這個函數(shù)的返回類型是UInt32。如下所示:
arc4random()
// 2,919,646,954
如果我們想生成一個指定范圍內(nèi)的整型隨機數(shù)箍铭,則可以使用arc4random() % upper_bound的方式绎晃,其中upper_bound指定的是上邊界蜜唾,如下處理:
arc4random() % 10
不過使用這種方法,在upper_bound不是2的冪次方時庶艾,會產(chǎn)生一個所謂Modulo bias(模偏差)的問題袁余。
我們在控制臺中通過man arc4random命令,可以查看arc4random的文檔咱揍,有這么一條:
arc4random_uniform() will return a uniformly distributed random number less than upper_bound. arc4random_uniform() is recommended over constructions like ‘’arc4random() % upper_bound’‘ as it avoids “modulo bias” when the upper bound is not a power of two.
因此可以使用arc4random_uniform颖榜,它接受一個UInt32類型的參數(shù),指定隨機數(shù)區(qū)間的上邊界upper_bound,該函數(shù)生成的隨機數(shù)范圍是[0, upper_bound)掩完,如下所示:
arc4random_uniform(10)
而如果想指定區(qū)間的最小值(如隨機數(shù)區(qū)間在[5, 100))噪漾,則可以如下處理:
let max: UInt32 = 100
let min: UInt32 = 5
arc4random_uniform(max - min) + min
當然,在Swift中也可以使用傳統(tǒng)的C函數(shù)rand與random且蓬。不過這兩個函數(shù)有如下幾個缺點:
這兩個函數(shù)都需要初始種子欣硼,通常是以當前時間來確定。
這兩個函數(shù)的上限在RAND_MAX=0X7fffffff(2147483647)恶阴,是arc4random的一半诈胜。
rand函數(shù)以有規(guī)律的低位循環(huán)方式實現(xiàn),更容易預測
我們以rand為例冯事,看看其使用:
srand(UInt32(time(nil)))
// 種子,random對應的是srandom
rand()
// 1,314,695,483
rand() % 10
64位整型隨機數(shù)
在大部分應用中焦匈,上面講到的幾個函數(shù)已經(jīng)足夠滿足我們獲取整型隨機數(shù)的需求了。不過我們看看它們的函數(shù)聲明昵仅,可以發(fā)現(xiàn)這些函數(shù)主要是針對32位整型來操作的缓熟。如果我們需要生成一個64位的整型隨機數(shù)呢?畢竟現(xiàn)在的新機器都是支持64位的了岩饼。
目前貌似沒有現(xiàn)成的函數(shù)來生成64位的隨機數(shù)荚虚,不過jstn在stackoverflow上為我們分享了他的方法薛夜。我們一起來看看籍茧。
他首先定義了一個泛型函數(shù),如下所示:
func arc4random (type: T.Type) -> T {
var r: T = 0
arc4random_buf(&r, UInt(sizeof(T)))
return r
}
這個函數(shù)中使用了arc4random_buf來生成隨機數(shù)梯澜。讓我們通過man arc4random_buf來看看這個函數(shù)的定義:
arc4random_buf() function fills the region buf of length nbytes with ARC4-derived random data.
這個函數(shù)使用ARC4加密的隨機數(shù)來填充該函數(shù)第二個參數(shù)指定的長度的緩存區(qū)域寞冯。因此,如果我們傳入的是sizeof(UInt64)晚伙,該函數(shù)便會生成一個隨機數(shù)來填充8個字節(jié)的區(qū)域吮龄,并返回給r。那么64位的隨機數(shù)生成方法便可以如下實現(xiàn):
extension UInt64 {
static func random(lower: UInt64 = min, upper: UInt64 = max) -> UInt64 {
var m: UInt64
let u = upper - lower
var r = arc4random(UInt64)
if u > UInt64(Int64.max) {
m = 1 + ~u
} else{
m = ((max - (u * 2)) + 1) % u
}
while r < m {
r = arc4random(UInt64)
}
return (r % u) + lower
}
}
我們來試用一下:
UInt64.random()
// 4758246381445086013
當然jstn還提供了Int64咆疗,UInt32漓帚,Int32的實現(xiàn),大家可以腦補一下午磁。
浮點型隨機數(shù)
如果需要一個浮點值的隨機數(shù)尝抖,則可以使用drand48函數(shù),這個函數(shù)產(chǎn)生一個[0.0, 1.0]區(qū)間中的浮點數(shù)迅皇。這個函數(shù)的返回值是Double類型昧辽。其使用如下所示:
srand48(Int(time(nil)))
drand48()
// 0.396464773760275
記住這個函數(shù)是需要先調(diào)用srand48生成一個種子的初始值。