0.1.0 • Published 4 years ago

wrandom v0.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

wrandom

Get a random element from a weighted array.

Installation

$ npm install wrandom

Usage

const wrandom = require('wrandom');
let arr = ['foo', 'bar', 'baz'];

// first argument is an array of values
// second argument is an array of relative weights
wrandom(arr, [4, 6, 10]); // same weights as [0.2, 0.3, 0.5]

Omitting the second argument returns a random element from the array, assuming an even weighting:

wrandom(arr); // same weights as [1, 1, 1]

License

Released under the terms of the MIT license. See LICENSE.