wrg
v1.0.0
Published
Generate random array indices weighted by given scores.
Downloads
6
Maintainers
Readme
Weighted Random Generator
Generate random array indices weighted by given scores. Optimized for use-cases when weights are rarely or never changing.
Usage
The package exposes a factory function which takes an array of weight scores and returns the random generator function.
const wrg = require('wrg'),
random = wrg([ 2, 5, 3 ]),
results = [ 0, 0, 0 ]
for (let i = 0; i < 1000; i++)
results[ random() ]++
console.log(results)
The result will be similar to the following:
[ 197, 488, 315 ]
Installation
With npm:
npm install wrg
Tests & benchmarks
Run unit tests:
npm test
Run unit tests and create coverage report:
npm run cover
Run benchmark:
npm run benchmark
License
Credits
This is an optimised version of Brandon Mills' weighted-random package.