wrg

1.0.0 • Public • Published

view on npm downloads per month node version build status test coverage license

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

MIT

Credits

This is an optimised version of Brandon Mills' weighted-random package.

Package Sidebar

Install

npm i wrg

Weekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • schwarzkopfb