wrandom

Get a random element from a weighted array

Usage no npm install needed!

<script type="module">
  import wrandom from 'https://cdn.skypack.dev/wrandom';
</script>

README

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.