Array

struct Array<Element> : _DestructorSafeContainer
  • Given an vector of weighted probabilities, this property return a random index considering the given probabilities. The vector doesn’t need to be normalized, every value will be taken into account.

    let probabilities = [0.2, 0.3, 0.1, 0.4]
    (0...1000).forEach {
        print(probabilities.weightedRandomIndex)
    }
    // This is most likely to print: 20% = 0, 30% 1, 10% = 2, 40% = 3
    
    Non-normalized vectors can also be used.
    

    Declaration

    Swift

    public var weightedRandomIndex: Index { get }