Swift 3:如何计算有利于偏向的随机数

Jos*_*han 3 random swift swift3

让我们说我正在计算1到100的随机数.我希望数字是随机选择的,但是我可以设置偏差,更可能选择中心.因此,如果我做随机样本可以说一千次,那么将会更频繁地选择中心数字.它选择中心的数量应该基于我在didHitChanceOf函数中设置的数字.做这个的最好方式是什么?

我目前的代码没有做到这一点,甚至是随机性的

当前非偏置随机数码(Swift 3)

extension Int
{
    static func random(range: ClosedRange<Int> ) -> Int
    {
        var offset = 0

        if range.lowerBound < 0   // allow negative ranges
        {
            offset = abs(range.lowerBound)
        }

        let mini = UInt32(range.lowerBound + offset)
        let maxi = UInt32(range.upperBound   + offset)

        return Int(mini + arc4random_uniform(maxi - mini)) - offset
    }
}

func didHitChanceOf(chance: Double) -> Bool{
        let random = Int.random(range: 0...100)
        if(Double(random) < chance){ //If the conversion rate is 20%, then only 20% of the time will the random number be less than the conversion rate.
            return true
        }else{
            return false
        }
    }
var adwordsClicks = 500
let adwordsConversionRate = 20
var adwordsConversions = 0
for _ in 0...adwordsClicks {
            if(didHitChanceOf(chance: adwordsConversionRate) == true){
                adwordsConversions = adwordsConversions + 1
            }
        }
Run Code Online (Sandbox Code Playgroud)

Cod*_*ent 7

你可以使用GKGaussianDistributionGameKit的(也就是正态分布)来做到这一点.您将需要2个参数:( mean您想要的"中心")和deviation(它应该从中心传播的距离):

import GameKit

func random(count: Int, in range: ClosedRange<Int>, mean: Int, deviation: Int) -> [Int] {
    guard count > 0 else { return [] }

    let randomSource = GKARC4RandomSource()
    let randomDistribution = GKGaussianDistribution(randomSource: randomSource, mean: Float(mean), deviation: Float(deviation))

    // Clamp the result to within the specified range
    return (0..<count).map { _ in
        let rnd = randomDistribution.nextInt()

        if rnd < range.lowerBound {
            return range.lowerBound
        } else if rnd > range.upperBound {
            return range.upperBound
        } else {
            return rnd
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用法和测试:

let arr = random(count: 1_000_000, in: 0...100, mean: 70, deviation: 10)

let summary = NSCountedSet(array: arr)
for i in 0...100 {
    print("\(i): \(summary.count(for: i))")
}
Run Code Online (Sandbox Code Playgroud)

您可以看到大约70的值具有最高计数