UNPKG

@rbxts/weighted-random-picker

Version:

Generates functions which pick a random element of an array based upon their assigned relative probabilities.

25 lines (18 loc) 653 B
# Weighted Random Picker Generates option-picker functions from relative probabilities: ```ts // When relative probabilities are not provided, it gives equal weight to each option const CoinFlip = new RandomPicker(["Heads", "Tails"]) print(CoinFlip()) // "Heads" print(CoinFlip()) // "Tails" const DiceRoll = new RandomPicker([1, 2, 3, 4, 5, 6]) const CoinFlipOrDiceRoll = new RandomPicker( // calls CoinFlip over DiceRoll at a 3:1 ratio // in other words, CoinFlip gets set to 0.75, DiceRoll becomes 0.25 [CoinFlip, DiceRoll], [ 3, 1] ) CoinFlipOrDiceRoll()() // 1 CoinFlipOrDiceRoll()() // Heads CoinFlipOrDiceRoll()() // 5 ```