這里利用 Darwin framework 中提供的隨機數(shù)生成函數(shù):
private func inputRandomData() -> Double {
let num = 1 + arc4random_uniform(10)
return Double(num) / 10.0
}
上面的代碼的作用是生成從 0.1 到 1.0 之間的隨機數(shù).
其中 arc4random_uniform(10)
中參數(shù)指定隨機數(shù)的最大值不超過多少, 比如指定10的話, 隨機數(shù)范圍就是 0 到 9 的 UInt32 類型數(shù)值.
可以使用 arc4random
來生成從 0 到 UInt32.Max 的隨機數(shù).
可以使用 drand48()
生成 0.0 到 1.0 之間的 Double 類型隨機數(shù).