Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/ricardomatias/roll
- Owner: ricardomatias
- License: mit
- Created: 2019-08-26T12:06:42.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T02:06:29.000Z (about 2 years ago)
- Last Synced: 2024-04-26T22:20:17.640Z (9 months ago)
- Topics: javascript, probability, probability-distribution, random
- Language: JavaScript
- Size: 1.01 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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 togetherconst 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.