我正在寻找一种高效的方法来生成大数(包括浮点类型!)在swift中,具有任意范围(甚至可以是UInt.max
或Int.max
)
我看到的所有现有问题要么崩溃,要么大范围(UInt.max
)或不支持范围。我知道您可以从/dev/urandom
中读取随机字节,但这无助于将这些值限制在给定的时间间隔内(而且我很确定循环直到循环无效)。
最佳答案
以下是UInt
、Int
和Double
的可能解决方案
所有这些类型。它是作为扩展方法编写的
(现在为swift 2更新),但对于全局函数也可以这样做。
注意arc4random_uniform()
只产生32位数字,因此
如果Int
/UInt
是64位整数,则不能使用
对于所有OSX机器和所有更新的iOS设备)。
对于UInt
我们使用https://stackoverflow.com/a/26550169/1187415中的技术
(这只是https://stackoverflow.com/a/10989061/1187415的快速翻译)。
范围涵盖UInt
的全部范围的情况单独处理。
extension UInt {
static func random(minValue minValue : UInt, maxValue : UInt) -> UInt {
precondition(minValue <= maxValue, "attempt to call random() with minValue > maxValue")
if minValue == UInt.min && maxValue == UInt.max {
// Random number in the full range of UInt:
var rnd : UInt = 0
arc4random_buf(&rnd, sizeofValue(rnd))
return rnd
} else {
// Compute random number in the range 0 ... (maxValue-minValue),
// using the technique from
// https://stackoverflow.com/a/26550169/1187415, https://stackoverflow.com/a/10989061/1187415
// and avoiding the "modulo bias problem":
let range = maxValue - minValue + 1
let randLimit = UInt.max - UInt.max % range
var rnd : UInt = 0
repeat {
arc4random_buf(&rnd, sizeofValue(rnd))
} while rnd >= randLimit
rnd = rnd % range
// Transform `rnd` back to the range minValue ... maxValue:
return minValue + rnd
}
}
}
示例:
let u1 = UInt.random(minValue: 1000, maxValue: 2000)
let u2 = UInt.random(minValue: UInt.min, maxValue: UInt.max)
可以使用
溢出运算符和
bitPattern:
转换:extension Int {
static func random(minValue minValue : Int, maxValue : Int) -> Int {
precondition(minValue <= maxValue, "attempt to call random() with minValue > maxValue")
// Compute unsigned random number in the range 0 ... (maxValue-minValue):
let diff = UInt(bitPattern: maxValue &- minValue)
let rnd = UInt.random(minValue: 0, maxValue: diff)
// Transform `rnd` back to the range minValue ... maxValue:
return minValue &+ Int(bitPattern: rnd)
}
}
示例:
let i1 = Int.random(minValue: -1000, maxValue: 1000)
let i2 = Int.random(minValue: Int.min, maxValue: Int.max)
最后,直接实现
Double
:extension Double {
static func random(minValue minValue : Double, maxValue : Double) -> Double {
precondition(minValue <= maxValue, "attempt to call random() with minValue > maxValue")
// Random floating point number in the range 0.0 ... 1.0:
let rnd = Double(UInt.random(minValue: 0, maxValue: UInt.max))/Double(UInt.max)
// Scale to range minValue ... maxValue:
return minValue + rnd * (maxValue - minValue)
}
}
例子:
let d = Double.random(minValue: 10.5, maxValue: 123.5)
SWIFT 3的更新:
extension UInt {
static func random(minValue: UInt, maxValue: UInt) -> UInt {
precondition(minValue <= maxValue, "attempt to call random() with minValue > maxValue")
if minValue == UInt.min && maxValue == UInt.max {
// Random number in the full range of UInt:
var rnd: UInt = 0
arc4random_buf(&rnd, MemoryLayout.size(ofValue: rnd))
return rnd
} else {
// Compute random number in the range 0 ... (maxValue-minValue),
// using the technique from
// https://stackoverflow.com/a/26550169/1187415, https://stackoverflow.com/a/10989061/1187415
// and avoiding the "modulo bias problem":
let range = maxValue - minValue + 1
let randLimit = UInt.max - UInt.max % range
var rnd: UInt = 0
repeat {
arc4random_buf(&rnd, MemoryLayout.size(ofValue: rnd))
} while rnd >= randLimit
rnd = rnd % range
// Transform `rnd` back to the range minValue ... maxValue:
return minValue + rnd
}
}
}
extension Int {
static func random(minValue: Int, maxValue: Int) -> Int {
precondition(minValue <= maxValue, "attempt to call random() with minValue > maxValue")
// Compute unsigned random number in the range 0 ... (maxValue-minValue):
let diff = UInt(bitPattern: maxValue &- minValue)
let rnd = UInt.random(minValue: 0, maxValue: diff)
// Transform `rnd` back to the range minValue ... maxValue:
return minValue &+ Int(bitPattern: rnd)
}
}
extension Double {
static func random(minValue: Double, maxValue: Double) -> Double {
precondition(minValue <= maxValue, "attempt to call random() with minValue > maxValue")
// Random floating point number in the range 0.0 ... 1.0:
let rnd = Double(UInt.random(minValue: 0, maxValue: UInt.max))/Double(UInt.max)
// Scale to range minValue ... maxValue:
return minValue + rnd * (maxValue - minValue)
}
}