Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ricardomatias/roll

Let's you pick an element randomly based on a chosen probability distribution
https://github.com/ricardomatias/roll

javascript probability probability-distribution random

Last synced: 14 days ago
JSON representation

Let's you pick an element randomly based on a chosen probability distribution

Awesome Lists containing this project

README

        

# roll [![Build Status](https://travis-ci.com/ricardomatias/roll.svg?branch=master)](https://travis-ci.com/ricardomatias/roll)

Let's you pick an element randomly based on a chosen probability distribution

```js
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](https://npmjs.com/) to install.

```sh
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




increasing(k, precision)Array.<String>


Creates an increasing probability distribution




equal(k, precision)Array.<String>


Creates an equal probability distribution



### 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](http://github.com/ricardomatias/ring/blob/master/LICENSE.md) for details.