mercredi 13 juin 2018

Probability function into array without repeats

So I have this method that takes the probabilities of all these variable and puts them into a function returns me an array of a random number of the variables, and those variables who made it out on the other side did so through their probability.

let n = Int(arc4random_uniform(8))

var a:Double = 0.3
var b:Double = 0.2
var c:Double = 0.2
var d:Double = 0.4
var e:Double = 0.2
var f:Double = 0.2
var g:Double = 0.2
var h:Double = 0.3
var i:Double = 0.2
var j:Double = 0.2
var k: [Int] = []

for _ in 0...n {
    func randomNumber(probabilities: [Double]) -> Int {
        let sum = probabilities.reduce(0, +)
        let rnd = sum * Double(arc4random_uniform(UInt32.max)) / Double(UInt32.max)
        var accum = 0.0
        for (i, p) in probabilities.enumerated() {
            accum += p
            if rnd < accum {
                return i
            }}
        return (probabilities.count - 1)
    }

    k.append(randomNumber(probabilities: [a, b, c, d, e, f, g, h, i, j]))

}

So var a represents 0, var b represents 1 and so on. My question is how do I make it so there are no repeats. So once 1 is selected once, it cant be selected again. Thanks!




Aucun commentaire:

Enregistrer un commentaire