Gathering detailed insights and metrics for random
Gathering detailed insights and metrics for random
Gathering detailed insights and metrics for random
Gathering detailed insights and metrics for random
Seedable random number generator supporting many common distributions.
npm install random
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.5
Supply Chain
99.6
Quality
87.5
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
210 Stars
207 Commits
29 Forks
6 Watchers
2 Branches
7 Contributors
Updated on Jul 12, 2025
Latest Version
5.4.1
Package Id
random@5.4.1
Unpacked Size
166.74 kB
Size
34.52 kB
File Count
9
NPM Version
11.3.0
Node Version
24.2.0
Published on
Jul 12, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Seedable random number generator supporting many common distributions.
Welcome to the most random module on npm! 😜
seedrandom
which hasn't been updated in over 5 years1npm install random
1import random from 'random' 2 3// quick uniform shortcuts 4random.float((min = 0), (max = 1)) // uniform float in [ min, max ) 5random.int((min = 0), (max = 1)) // uniform integer in [ min, max ] 6random.boolean() // true or false 7 8// uniform distribution 9random.uniform((min = 0), (max = 1)) // () => [ min, max ) 10random.uniformInt((min = 0), (max = 1)) // () => [ min, max ] 11random.uniformBoolean() // () => [ false, true ] 12 13// normal distribution 14random.normal((mu = 0), (sigma = 1)) 15random.logNormal((mu = 0), (sigma = 1)) 16 17// bernoulli distribution 18random.bernoulli((p = 0.5)) 19random.binomial((n = 1), (p = 0.5)) 20random.geometric((p = 0.5)) 21 22// poisson distribution 23random.poisson((lambda = 1)) 24random.exponential((lambda = 1)) 25 26// misc distribution 27random.irwinHall(n) 28random.bates(n) 29random.pareto(alpha)
For convenience, several common uniform samplers are exposed directly:
1random.float() // 0.2149383367670885 2random.int(0, 100) // 72 3random.boolean() // true 4 5// random array item 6random.choice([1, true, 'foo']) // 'foo' 7 8// sample multiple items without replacement 9random.sample([1, true, 'foo'], 2) // [true, 'foo'] 10 11const dist = random.sampler([1, true, 'foo'], 2) 12dist() // [true, 'foo'] 13dist() // ['foo', 1] 14dist() // [1, true] 15 16// shuffle arrays 17random.shuffle([1, true, 'foo']) // ['foo', 1, true] 18 19const dist = random.shuffler([1, true, 'foo']) 20dist() // [true, 'foo', 1] 21dist() // ['foo', true, 1] 22dist() // [1, 'foo', true]
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.
1// create a normal distribution with default params (mu=0 and sigma=1) 2const normal = random.normal() 3normal() // 0.4855465422678824 4normal() // -0.06696771815439678 5normal() // 0.7350852689834705 6 7// create a poisson distribution with default params (lambda=1) 8const poisson = random.poisson() 9poisson() // 0 10poisson() // 4 11poisson() // 1
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:
1// change the underlying pseudo random number generator seed. 2// by default, Math.random is used as the underlying PRNG, but it is not seedable, 3// so if a seed is given, we use an ARC4 PRNG (the same one used by `seedrandom`). 4random.use('my-seed') 5 6// create a new independent random number generator with a different seed 7const rng = random.clone('my-new-seed') 8 9// create a third independent random number generator using a custom PRNG 10import seedrandom from 'seedrandom' 11const rng2 = random.clone(seedrandom('kitty-seed'))
You can also instantiate a fresh instance of Random
:
1import { Random } from 'random' 2 3const rng = new Random() // (uses Math.random) 4const rng2 = new Random('my-seed-string') 5const rng3 = new Random(() => { 6 /* custom PRNG */ return Math.random() 7})
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)
args
...anyExample:
1import random from 'random' 2 3random.use('example_seedrandom_string') 4// or 5random.use(seedrandom('kittens')) 6// or 7random.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
ArrayGenerates 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
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
)Generates a Weibull distribution.
Type: function (lambda, k): function
Distributions
Generators
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.
MIT © Travis Fischer
Support my OSS work by following me on twitter
No vulnerabilities found.
Reason
12 commit(s) and 3 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 5/21 approved changesets -- score normalized to 2
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
13 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More