1.0.0 • Published 9 years ago
wrg v1.0.0
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 wrgTests & benchmarks
Run unit tests:
npm testRun unit tests and create coverage report:
npm run coverRun benchmark:
npm run benchmarkLicense
Credits
This is an optimised version of Brandon Mills' weighted-random package.
1.0.0
9 years ago