这是一个Swift实现,受各种答案的影响很大,这些答案会生成具有给定(数字)分布的随机数
对于 Swift 4.2 / Xpre 10 和更高版本(嵌入式解释):
func randomNumber(probabilities: [Double]) -> Int { // Sum of all probabilities (so that we don't have to require that the sum is 1.0): let sum = probabilities.reduce(0, +) // Random number in the range 0.0 <= rnd < sum : let rnd = Double.random(in: 0.0 ..< sum) // Find the first interval of accumulated probabilities into which `rnd` falls: var accum = 0.0 for (i, p) in probabilities.enumerated() { accum += p if rnd < accum { return i } } // This point might be reached due to floating point inaccuracies: return (probabilities.count - 1)}例子:
let x = randomNumber(probabilities: [0.2, 0.3, 0.5])
返回概率为0.2的0,概率为0.3的1,概率为0.5的2。
let x = randomNumber(probabilities: [1.0, 2.0])
以1/3的概率返回0,以2/3的概率返回1。
对于 Swift 3 / Xpre 8:
func randomNumber(probabilities: [Double]) -> Int { // Sum of all probabilities (so that we don't have to require that the sum is 1.0): let sum = probabilities.reduce(0, +) // Random number in the range 0.0 <= rnd < sum : let rnd = sum * Double(arc4random_uniform(UInt32.max)) / Double(UInt32.max) // Find the first interval of accumulated probabilities into which `rnd` falls: var accum = 0.0 for (i, p) in probabilities.enumerated() { accum += p if rnd < accum { return i } } // This point might be reached due to floating point inaccuracies: return (probabilities.count - 1)}对于 Swift 2 / Xpre 7:
func randomNumber(probabilities probabilities: [Double]) -> Int { // Sum of all probabilities (so that we don't have to require that the sum is 1.0): let sum = probabilities.reduce(0, combine: +) // Random number in the range 0.0 <= rnd < sum : let rnd = sum * Double(arc4random_uniform(UInt32.max)) / Double(UInt32.max) // Find the first interval of accumulated probabilities into which `rnd` falls: var accum = 0.0 for (i, p) in probabilities.enumerate() { accum += p if rnd < accum { return i } } // This point might be reached due to floating point inaccuracies: return (probabilities.count - 1)}


