@ricardomatias/roll
v0.2.0
Published
Let's you pick an element randomly based on a chosen probability distribution
Downloads
10
Maintainers
Readme
roll
Let's you pick an element randomly based on a chosen probability distribution
import { roll, distribute } from '@ricardomatias/roll';
import seedRandom from 'seed-random'; // works nicely together
const rng = seedRandom('roll');
const AM7 = [ 'A', 'C', 'E', 'G' ];
const equalOdds = distribute.equal(AM7.length);
const decreasingOdds = distribute.decreasing(AM7.length);
const increasingOdds = distribute.increasing(AM7);
roll(AM7, equalOdds, rng); // => G
roll(AM7, equalOdds, rng); // => C
roll(AM7, decreasingOdds, rng); // => A
roll(AM7, increasingOdds, rng); // => E
Install
Use npm to install.
npm install @ricardomatias/roll --save
Usage
roll
roll(elements, probabilities, rng) ⇒ Object | Null
Pick an element randomly based on a chosen probability distribution
| Param | Type | Description | | --- | --- | --- | | elements | Array | | | probabilities | Array.<String> | | | rng | function | Random Number Generator |
distribute
decreasing(k, precision) ⇒ Array.<String>
Creates a decreasing probability distribution
| Param | Type | | --- | --- | | k | Number | Array.<Number> | | [precision] | Number | 3 |
increasing(k, precision) ⇒ Array.<String>
Creates an increasing probability distribution
| Param | Type | | --- | --- | | k | Number | Array.<Number> | | [precision] | Number | 3 |
equal(k, precision) ⇒ Array.<String>
Creates an equal probability distribution
| Param | Type | | --- | --- | | k | Number | Array.<Number> | | [precision] | Number | 3 |
License
MIT, see LICENSE.md for details.