Swift隨機數獲取

整型隨機數
如果我們想要一個整型的隨機數,則可以考慮用arc4random系列函數。我們可以通過man arc4random命令來看一下這個函數的定義:
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生成器(請腦補),產生一個[0, 2^32)區間的隨機數(注意是左閉右開區間)。這個函數的返回類型是UInt32。如下所示:

arc4random()                
// 2,919,646,954

如果我們想生成一個指定范圍內的整型隨機數,則可以使用arc4random() % upper_bound的方式,其中upper_bound指定的是上邊界,如下處理:

arc4random() % 10            

不過使用這種方法,在upper_bound不是2的冪次方時,會產生一個所謂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類型的參數,指定隨機數區間的上邊界upper_bound,該函數生成的隨機數范圍是[0, upper_bound),如下所示:

arc4random_uniform(10)       

而如果想指定區間的最小值(如隨機數區間在[5, 100)),則可以如下處理:

let max: UInt32 = 100

let min: UInt32 = 5

arc4random_uniform(max - min) + min           

當然,在Swift中也可以使用傳統的C函數rand與random。不過這兩個函數有如下幾個缺點:
這兩個函數都需要初始種子,通常是以當前時間來確定。
這兩個函數的上限在RAND_MAX=0X7fffffff(2147483647),是arc4random的一半。
rand函數以有規律的低位循環方式實現,更容易預測
我們以rand為例,看看其使用:

srand(UInt32(time(nil)))            
// 種子,random對應的是srandom

rand()                              
// 1,314,695,483

rand() % 10                          

64位整型隨機數
在大部分應用中,上面講到的幾個函數已經足夠滿足我們獲取整型隨機數的需求了。不過我們看看它們的函數聲明,可以發現這些函數主要是針對32位整型來操作的。如果我們需要生成一個64位的整型隨機數呢?畢竟現在的新機器都是支持64位的了。
目前貌似沒有現成的函數來生成64位的隨機數,不過jstn在stackoverflow上為我們分享了他的方法。我們一起來看看。
他首先定義了一個泛型函數,如下所示:

func arc4random  (type: T.Type) -> T {  
  var r: T = 0
  arc4random_buf(&r, UInt(sizeof(T)))   
  return r
}

這個函數中使用了arc4random_buf來生成隨機數。讓我們通過man arc4random_buf來看看這個函數的定義:
arc4random_buf() function fills the region buf of length nbytes with ARC4-derived random data.
這個函數使用ARC4加密的隨機數來填充該函數第二個參數指定的長度的緩存區域。因此,如果我們傳入的是sizeof(UInt64),該函數便會生成一個隨機數來填充8個字節的區域,并返回給r。那么64位的隨機數生成方法便可以如下實現:

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的實現,大家可以腦補一下。
浮點型隨機數
如果需要一個浮點值的隨機數,則可以使用drand48函數,這個函數產生一個[0.0, 1.0]區間中的浮點數。這個函數的返回值是Double類型。其使用如下所示:

srand48(Int(time(nil)))

drand48()                       
// 0.396464773760275

記住這個函數是需要先調用srand48生成一個種子的初始值。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容