weighted-arrays

1.2.0 • Public • Published

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

Dependents (0)

Package Sidebar

Install

npm i weighted-arrays

Weekly Downloads

2

Version

1.2.0

License

MIT

Unpacked Size

5.4 kB

Total Files

5

Last publish

Collaborators

  • sophietk