random
Seedable random number generator supporting many common distributions.
Welcome to the most random module on npm! 😜
Highlights
- Simple TS API with zero dependencies
- Seedable
- Plugin support for different pseudo random number generators
- Includes many common distributions
- uniform, normal, poisson, bernoulli, etc
- Replacement for
seedrandom
which hasn't been updated in over 5 years - Supports all modern JS/TS runtimes
Install
npm install random
Usage
import random from 'random'
random.float((min = 0), (max = 1))
random.int((min = 0), (max = 1))
random.boolean()
random.uniform((min = 0), (max = 1))
random.uniformInt((min = 0), (max = 1))
random.uniformBoolean()
random.normal((mu = 0), (sigma = 1))
random.logNormal((mu = 0), (sigma = 1))
random.bernoulli((p = 0.5))
random.binomial((n = 1), (p = 0.5))
random.geometric((p = 0.5))
random.poisson((lambda = 1))
random.exponential((lambda = 1))
random.irwinHall(n)
random.bates(n)
random.pareto(alpha)
For convenience, several common uniform samplers are exposed directly:
random.float()
random.int(0, 100)
random.boolean()
random.choice([1, true, 'foo'])
All distribution methods return a thunk (function with no params), which will return a series of independent, identically distributed random variables from the specified distribution.
const normal = random.normal()
normal()
normal()
normal()
const poisson = random.poisson()
poisson()
poisson()
poisson()
Note that returning a thunk here is more efficient when generating multiple samples from the same distribution.
You can change the underlying PRNG or its seed as follows:
random.use('my-seed')
const rng = random.clone('my-new-seed')
import seedrandom from 'seedrandom'
const rng2 = random.clone(seedrandom('kitty-seed'))
You can also instantiate a fresh instance of Random
:
import { Random } from 'random'
const rng = new Random()
const rng2 = new Random('my-seed-string')
const rng3 = new Random(() => {
return Math.random()
})
API
Table of Contents
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, default Math.random
)
Type: function ()
Creates a new Random
instance, optionally specifying parameters to
set a new seed.
Type: function (args, seed, opts): Random
args
...anyseed
string? Optional seed for new RNG.opts
object? Optional config for new RNG options.
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)
Example:
import random from 'random'
random.use('example_seedrandom_string')
random.use(seedrandom('kittens'))
random.use(Math.random)
Convenience wrapper around this.rng.next()
Returns a floating point number in [0, 1).
Type: function (): number
Samples a uniform random floating point number, optionally specifying
lower and upper bounds.
Convence wrapper around random.uniform()
Type: function (min, max): number
min
number Lower bound (float, inclusive) (optional, default 0
)max
number Upper bound (float, exclusive) (optional, default 1
)
Samples a uniform random integer, optionally specifying lower and upper
bounds.
Convence wrapper around random.uniformInt()
Type: function (min, max): number
min
number Lower bound (integer, inclusive) (optional, default 0
)max
number Upper bound (integer, inclusive) (optional, default 1
)
Samples a uniform random integer, optionally specifying lower and upper
bounds.
Convence wrapper around random.uniformInt()
Type: function (min, max): number
min
number Lower bound (integer, inclusive) (optional, default 0
)max
number Upper bound (integer, inclusive) (optional, default 1
)
Samples a uniform random boolean value.
Convence wrapper around random.uniformBoolean()
Type: function (): boolean
Samples a uniform random boolean value.
Convence wrapper around random.uniformBoolean()
Type: function (): boolean
Returns an item chosen uniformly at random from the given array.
Convence wrapper around random.uniformInt()
Type: function choice <T> (array: Array<T>): T | undefined
array
Array Array of items to sample from
Generates a Continuous uniform distribution.
Type: function (min, max): function
min
number Lower bound (float, inclusive) (optional, default 0
)max
number Upper bound (float, exclusive) (optional, default 1
)
Generates a Discrete uniform distribution.
Type: function (min, max): function
min
number Lower bound (integer, inclusive) (optional, default 0
)max
number Upper bound (integer, inclusive) (optional, default 1
)
Generates a Discrete uniform distribution,
with two possible outcomes, true
or `false.
This method is analogous to flipping a coin.
Type: function (): function
Generates a Normal distribution.
Type: function (mu, sigma): function
mu
number Mean (optional, default 0
)sigma
number Standard deviation (optional, default 1
)
Generates a Log-normal distribution.
Type: function (mu, sigma): function
mu
number Mean of underlying normal distribution (optional, default 0
)sigma
number Standard deviation of underlying normal distribution (optional, default 1
)
Generates a Bernoulli distribution.
Type: function (p): function
p
number Success probability of each trial. (optional, default 0.5
)
Generates a Binomial distribution.
Type: function (n, p): function
n
number Number of trials. (optional, default 1
)p
number Success probability of each trial. (optional, default 0.5
)
Generates a Geometric distribution.
Type: function (p): function
p
number Success probability of each trial. (optional, default 0.5
)
Generates a Poisson distribution.
Type: function (lambda): function
lambda
number Mean (lambda > 0) (optional, default 1
)
Generates an Exponential distribution.
Type: function (lambda): function
lambda
number Inverse mean (lambda > 0) (optional, default 1
)
Generates an Irwin Hall distribution.
Type: function (n): function
n
number Number of uniform samples to sum (n >= 0) (optional, default 1
)
Generates a Bates distribution.
Type: function (n): function
n
number Number of uniform samples to average (n >= 1) (optional, default 1
)
Generates a Pareto distribution.
Type: function (alpha): function
alpha
number Alpha (optional, default 1
)
Todo
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.
- jshash prngs
Credit
Thanks go to Andrew Moss for the TypeScript port and for helping to maintain this package.
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
Support my OSS work by following me on twitter