Package Exports
- implausible
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (implausible) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
implausible
implausible is a collection of pseudorandom number generators (PRNGs) and utilities powered by seedrandom.
Quick start
RunKit
RunKit provides one of the least difficult ways to get started:
CodePen
Declare imports and global binding in the JS section to get started:
import { prng } from 'https://unpkg.com/implausible@latest?module';
window.prng = prng;Run commands in the Console section:
prng();
// example output: 0.3722007770466942
Browsers
Insert the following element within the <head> tag of an HTML document:
<script src="https://unpkg.com/implausible@latest"></script>After the script is loaded, the implausible browser global is exposed:
implausible.prng();
// example output: 0.6736552471595748Node.js
With npm installed, run terminal command:
npm i implausibleOnce installed, declare method imports at the top of each JavaScript file they will be used.
ES2015
Recommended
import { prng, sample } from 'implausible';CommonJS
const { prng, sample } = require('implausible');Usage
Generate stochastic number
prng();
// example output: 0.4471833625387327
prng();
// example output: 0.18700348375416123...with a specific algorithm
Refer to the list of PRNG names for valid parameter { name } values.
prng({ name: 'xor4096' });
// example output: 0.7105067998636514Generate deterministic number
prng({ seed: 'hello.' });
// output: 0.9282578795792454
prng({ seed: 'hello.' });
// output: 0.9282578795792454...with a specific algorithm
Refer to the list of PRNG names for valid parameter { name } values.
prng({
name: 'xor4096',
seed: 'hello.',
});
// output: 0.9798525865189731Stochastic weighted sample
sample({
collection: {
heads: 1,
tails: 1,
},
});
// example output: 'tails'...with a specific algorithm
Refer to the list of PRNG names for valid parameter { name } values.
sample({
collection: {
heads: 1,
tails: 1,
},
name: 'alea',
});
// example output: 'heads'Deterministic weighted sample
sample({
collection: {
heads: 1,
tails: 1,
},
seed: 'hello.',
});
// output: 'tails'...with a specific algorithm
Refer to the list of PRNG names for valid parameter { name } values.
sample({
collection: {
heads: 1,
tails: 1,
},
name: 'tychei',
seed: 'hello.',
});
// output: 'heads'API
List of PRNG names
The following names of pseudorandom number generators (PRNGs) are valid String inputs for the optional { name } parameter:
aleaarc4(default)tycheixor128xor4096xorshift7xorwow
All undefined seed are automatically generated by arc4 before being piped to other generators in stochastic mode.
Visit seedrandom documentation for comparative statistics on period and performance.
prng() || prng({ name, seed })
Input
All parameters are optional properties of an optional Object.
| parameter | input type(s) | default | description |
|---|---|---|---|
name |
String |
arc4 |
Refer to the list of PRNG names for values. |
seed |
Number, String |
undefined (stochastic) |
Deterministic when provided, or stochastic when undefined. |
Output
Generates a Number equal to or greater than 0 and less than 1.
sample({ collection[, name][, seed] })
Input
All parameters are properties of an Object.
| parameter | input type(s) | default | description |
|---|---|---|---|
collection (required) |
Object of {String:Number} pairs |
Histogram where the relative probability of sampling a key is determined by its Number value. |
|
name |
String |
arc4 |
Refer to the list of PRNG names for values. |
seed |
Number, String |
undefined (stochastic) |
Deterministic when provided, or stochastic when undefined. |
Output
Generates a String weighted random sample from a collection key.
Credits
Thanks to David Bau and additional authors for distributing parent package seedrandom under the MIT license.