1.2.0 • Published 5 years ago
weighted-arrays v1.2.0
weighted-arrays
install it
npm i weighted-arrays
use it
const wa = require('weighted-arrays')
const array = [
{ value: 'a', weight: 0},
{ value: 'b', weight: 2.5},
{ value: 'c', weight: 10}
]
const getWeight = obj => obj.weight
const random = wa.random(array, getWeight)
// output: sometimes { value: 'b', weight: 2.5}, sometimes { value: 'c', weight: 10}, and never { value: 'a', weight: 0}
const probability = wa.probability(array, getWeight, array[1])
// output: 0.2