@rbxts/weighted-random-picker
v1.0.1
Published
Generates functions which pick a random element of an array based upon their assigned relative probabilities.
Downloads
4
Maintainers
Readme
Weighted Random Picker
Generates option-picker functions from relative probabilities:
// 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