整型隨機(jī)數(shù)
如果我們想要一個(gè)整型的隨機(jī)數(shù),則可以考慮用arc4random系列函數(shù)。我們可以通過man arc4random命令來看一下這個(gè)函數(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生成器(請腦補(bǔ)),產(chǎn)生一個(gè)[0, 2^32)區(qū)間的隨機(jī)數(shù)(注意是左閉右開區(qū)間)。這個(gè)函數(shù)的返回類型是UInt32。如下所示:
arc4random()
// 2,919,646,954
如果我們想生成一個(gè)指定范圍內(nèi)的整型隨機(jī)數(shù),則可以使用arc4random() % upper_bound的方式,其中upper_bound指定的是上邊界,如下處理:
arc4random() % 10
不過使用這種方法,在upper_bound不是2的冪次方時(shí),會(huì)產(chǎn)生一個(gè)所謂Modulo bias(模偏差)的問題。
我們在控制臺(tái)中通過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,它接受一個(gè)UInt32類型的參數(shù),指定隨機(jī)數(shù)區(qū)間的上邊界upper_bound,該函數(shù)生成的隨機(jī)數(shù)范圍是[0, upper_bound),如下所示:
arc4random_uniform(10)
而如果想指定區(qū)間的最小值(如隨機(jī)數(shù)區(qū)間在[5, 100)),則可以如下處理:
let max: UInt32 = 100
let min: UInt32 = 5
arc4random_uniform(max - min) + min
當(dāng)然,在Swift中也可以使用傳統(tǒng)的C函數(shù)rand與random。不過這兩個(gè)函數(shù)有如下幾個(gè)缺點(diǎn):
這兩個(gè)函數(shù)都需要初始種子,通常是以當(dāng)前時(shí)間來確定。
這兩個(gè)函數(shù)的上限在RAND_MAX=0X7fffffff(2147483647),是arc4random的一半。
rand函數(shù)以有規(guī)律的低位循環(huán)方式實(shí)現(xiàn),更容易預(yù)測
我們以rand為例,看看其使用:
srand(UInt32(time(nil)))
// 種子,random對應(yīng)的是srandom
rand()
// 1,314,695,483
rand() % 10
64位整型隨機(jī)數(shù)
在大部分應(yīng)用中,上面講到的幾個(gè)函數(shù)已經(jīng)足夠滿足我們獲取整型隨機(jī)數(shù)的需求了。不過我們看看它們的函數(shù)聲明,可以發(fā)現(xiàn)這些函數(shù)主要是針對32位整型來操作的。如果我們需要生成一個(gè)64位的整型隨機(jī)數(shù)呢?畢竟現(xiàn)在的新機(jī)器都是支持64位的了。
目前貌似沒有現(xiàn)成的函數(shù)來生成64位的隨機(jī)數(shù),不過jstn在stackoverflow上為我們分享了他的方法。我們一起來看看。
他首先定義了一個(gè)泛型函數(shù),如下所示:
func arc4random (type: T.Type) -> T {
var r: T = 0
arc4random_buf(&r, UInt(sizeof(T)))
return r
}
這個(gè)函數(shù)中使用了arc4random_buf來生成隨機(jī)數(shù)。讓我們通過man arc4random_buf來看看這個(gè)函數(shù)的定義:
arc4random_buf() function fills the region buf of length nbytes with ARC4-derived random data.
這個(gè)函數(shù)使用ARC4加密的隨機(jī)數(shù)來填充該函數(shù)第二個(gè)參數(shù)指定的長度的緩存區(qū)域。因此,如果我們傳入的是sizeof(UInt64),該函數(shù)便會(huì)生成一個(gè)隨機(jī)數(shù)來填充8個(gè)字節(jié)的區(qū)域,并返回給r。那么64位的隨機(jī)數(shù)生成方法便可以如下實(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
當(dāng)然jstn還提供了Int64,UInt32,Int32的實(shí)現(xiàn),大家可以腦補(bǔ)一下。
浮點(diǎn)型隨機(jī)數(shù)
如果需要一個(gè)浮點(diǎn)值的隨機(jī)數(shù),則可以使用drand48函數(shù),這個(gè)函數(shù)產(chǎn)生一個(gè)[0.0, 1.0]區(qū)間中的浮點(diǎn)數(shù)。這個(gè)函數(shù)的返回值是Double類型。其使用如下所示:
srand48(Int(time(nil)))
drand48()
// 0.396464773760275
記住這個(gè)函數(shù)是需要先調(diào)用srand48生成一個(gè)種子的初始值。