Package Exports
- random
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 (random) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
random
Seedable random number generator supporting many common distributions.
Highlights
Welcome to the most random module on npm! 😜
- Simple API (make easy things easy and hard things possible)
- Seedable based on entropy or user input
- Plugin support for different pseudo random number generators (PRNGs)
- Sample from many common distributions
- uniform, normal, poisson, bernoulli, etc
- Validates all user input via ow
- Integrates with seedrandom
- Supports node >= 6 and browser
Install
NOTE: This module is currently an active WIP, and we do not recommend using it yet.
npm install --save randomUsage
const random = require('random')
// quick uniform shortcuts
random.float(min = 0, max = 1) // uniform float in [ min, max )
random.int(min = 0, max = 1) // uniform integer in [ min, max ]
random.boolean() // true or false
// uniform
random.uniform(min = 0, max = 1) // () => [ min, max )
random.uniformInt(min = 0, max = 1) // () => [ min, max ]
random.uniformBoolean() // () => [ false, true ]
// normal
random.normal(mu = 0, sigma = 1)
random.logNormal(mu = 0, sigma = 1)
// bernoulli
random.bernoulli(p)
random.binomial(n, p)
random.geometric(p)
// poisson
random.poisson()
random.exponential(lambda = 1)
// misc
random.irwinHall()
random.bates()
random.pareto(alpha)For convenience, several common uniform samplers are exposed directly:
random.float() // 0.2149383367670885
random.int(0, 100) // 72
random.boolean() // trueAll distribution methods return a thunk (function with no params), which will return a series of independent, identically distributed random variables from the specified distribution.
Note that returning a thunk here is more efficient when generating multiple samples from the same distribution.
// create a normal distribution with default params (mu=1 and sigma=0)
const normal = random.normal()
normal() // 0.4855465422678824
normal() // -0.06696771815439678
normal() // 0.7350852689834705
// create a poisson distribution with default params (lambda=1)
const poisson = random.poisson()
poisson() // 0
poisson() // 4
poisson() // 1You can change the underlying PRNG or its seed as follows:
const seedrandom = require('seedrandom')
// change the underlying pseudo random number generator
// by default, Math.random is used as the underlying PRNG
random.use(seedrandom('foobar'))
// create a new independent random number generator
const rng = random.clone('my-new-seed')
// create a second independent random number generator and use a seeded PRNG
const rng2 = random.clone(seedrandom('kittyfoo'))
// replace Math.random with rng.uniform
rng.patch()
// restore original Math.random
rng.unpatch()API
Table of Contents
Random
Seedable random number generator supporting many common distributions.
Defaults to Math.random as its underlying pseudorandom number generator.
Type: function (rng)
rng(RNG | function) Underlying pseudorandom number generator. (optional, defaultMath.random)
rng
Type: function ()
clone
- See: RNG.clone
Creates a new Random instance, optionally specifying parameters to
set a new seed.
Type: function (args, seed, opts): Random
args...anyseedstring? Optional seed for new RNG.optsobject? Optional config for new RNG options.
use
Sets the underlying pseudorandom number generator used via
either an instance of seedrandom, a custom instance of RNG
(for PRNG plugins), or a string specifying the PRNG to use
along with an optional seed and opts to initialize the
RNG.
Type: function (args)
args...any
Example:
```js
const random = require('random')
random.use('xor128', 'foobar')
// or
random.use(seedrandom('kittens'))
// or
random.use(Math.random)
```patch
Patches Math.random with this Random instance's PRNG.
Type: function ()
unpatch
Restores a previously patched Math.random to its original value.
Type: function ()
next
Convenience wrapper around this.rng.next()
Returns a floating point number in [0, 1).
Type: function (): number
float
Samples a uniform random floating point number, optionally specifying lower and upper bounds.
Convence wrapper around random.uniform()
Type: function (min, max): number
minnumber Lower bound (float, inclusive) (optional, default0)maxnumber Upper bound (float, exclusive) (optional, default1)
int
Samples a uniform random integer, optionally specifying lower and upper bounds.
Convence wrapper around random.uniformInt()
Type: function (min, max): number
minnumber Lower bound (integer, inclusive) (optional, default0)maxnumber Upper bound (integer, inclusive) (optional, default1)
integer
Samples a uniform random integer, optionally specifying lower and upper bounds.
Convence wrapper around random.uniformInt()
Type: function (min, max): number
minnumber Lower bound (integer, inclusive) (optional, default0)maxnumber Upper bound (integer, inclusive) (optional, default1)
bool
Samples a uniform random boolean value.
Convence wrapper around random.uniformBoolean()
Type: function (): boolean
boolean
Samples a uniform random boolean value.
Convence wrapper around random.uniformBoolean()
Type: function (): boolean
normal
Generates a Normal distribution.
Type: function (args, mu, sigma): function
args...anymunumber Mean (optional, default0)sigmanumber Standard deviation (optional, default1)
logNormal
Generates a Log-normal distribution.
Type: function (args, mu, sigma): function
args...anymunumber Mean of underlying normal distribution (optional, default0)sigmanumber Standard deviation of underlying normal distribution (optional, default1)
bernoulli
Generates a Bernoulli distribution.
Type: function (args, p): function
args...anypnumber Success probability of each trial. (optional, default0.5)
bernoulli
Generates a Chi-squared distribution.
Type: function (args, k): function
args...anyknumber Degrees of freedom (k > 0) (optional, default1)
binomial
Generates a Binomial distribution.
Type: function (args, n, p): function
args...anynnumber Number of trials. (optional, default1)pnumber Success probability of each trial. (optional, default0.5)
geometric
Generates a Geometric distribution.
Type: function (args, p): function
args...anypnumber Success probability of each trial. (optional, default0.5)
poisson
Generates a Poisson distribution.
Type: function (args, lambda): function
args...anylambdanumber Mean (lambda > 0) (optional, default1)
exponential
Generates an Exponential distribution.
Type: function (args, lambda): function
args...anylambdanumber Inverse mean (lambda > 0) (optional, default1)
irwinHall
Generates a Gamma distribution.
Type: function (args, alpha, beta): function
args...anyalphanumber Shape (alpha > 0) (optional, default1)betanumber Rate (beta > 0) (optional, default1)
irwinHall
Generates an Irwin Hall distribution.
Type: function (args, n): function
args...anynnumber Number of uniform samples to sum (n >= 0)
bates
Generates a Bates distribution.
Type: function (args, n): function
args...anynnumber Number of uniform samples to average (n >= 1)
pareto
Generates a Pareto distribution.
Type: function (args, alpha): function
args...anyalphanumber Alpha
Todo
Distributions
- uniform
- uniformInt
- uniformBoolean
- normal
- logNormal
- chiSquared
- cauchy
- fischerF
- studentT
- bernoulli
- binomial
- negativeBinomial
- geometric
- poisson
- exponential
- gamma
- hyperExponential
- weibull
- beta
- laplace
- irwinHall
- bates
- pareto
Generators
- pluggable prng
- more prng from boost
- custom entropy
Misc
- browser support via rollup
- basic docs
- basic tests
- first release!
Related
- d3-random - D3's excellent random number generation library.
- seedrandom - Seedable pseudo random number generator.
- random-int - For the common use case of generating uniform random ints.
- random-float - For the common use case of generating uniform random floats.
- randombytes - Random crypto bytes for Node.js and the browser.
Credit
Huge shoutout to Roger Combs for donating the random npm package for this project!
Lots of inspiration from d3-random (@mbostock and @svanschooten).
Some distributions and PRNGs are ported from C++ boost::random.
License
MIT © Travis Fischer