Miss any of our Open RFC calls?Watch the recordings here! »

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

Install

npm i weighted-arrays

DownloadsWeekly Downloads

1

Version

1.2.0

License

MIT

Unpacked Size

5.4 kB

Total Files

5

Last publish

Collaborators

  • avatar