Global

Methods

baseGenerator(generator, sizeopt) → {*|Array}

Generates random values or arrays of random values given a sampling function and a size parameter.

Parameters:
Name Type Attributes Default Description
generator function

Function returning one sample on each call.

size number | Array | null <optional>
null

The shape of the output:

  • If null, a single value is returned.
  • If a number, a 1D array of the specified size is returned.
  • If an array (e.g., [m, n]), a multidimensional array of the specified shape is returned.
Source:
Throws:

Throws an error if the size parameter is not a non-negative integer, an array of them, or null.

Type
Error
Returns:

A single value, a 1D array or a nested array of the given shape.

Type
* | Array

bernoulli(popt, sizeopt) → {number|Array}

Draws samples from a Bernoulli distribution: 1 with probability p, 0 otherwise.

Parameters:
Name Type Attributes Default Description
p number <optional>
0.5

Success probability in [0, 1].

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

beta(a, b, sizeopt) → {number|Array}

Draws samples from a Beta distribution on [0, 1].

Parameters:
Name Type Attributes Default Description
a number

Alpha (> 0).

b number

Beta (> 0).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

betaPair(a, b) → {Array.<number>}

Beta deviate together with its complement, both computed without cancellation: returns [x, 1 - x] where 1 - x keeps full relative precision even when x is close to 1. Jöhnk's algorithm when both parameters are <= 1 (robust in log space), otherwise ratio of gammas.

Parameters:
Name Type Description
a number
b number
Source:
Returns:
Type
Array.<number>

betaSample(a, b) → {number}

Beta deviate.

Parameters:
Name Type Description
a number
b number
Source:
Returns:
Type
number

binomial(n, p, sizeopt) → {number|Array}

Generates random numbers or arrays of random numbers for a binomial distribution: the number of successes in n independent trials with success probability p.

Sampling is exact and fast for any n (inversion for small means, beta splitting otherwise).

Parameters:
Name Type Attributes Default Description
n number

Number of trials (non-negative integer).

p number

Probability of success in each trial, in [0, 1].

size number | Array | null <optional>
null

The shape of the output:

  • If null, a single number is returned.
  • If a number, a 1D array of the specified size is returned.
  • If an array (e.g., [m, n]), a multidimensional array of the specified shape is returned.
Source:
Throws:

If n is not a non-negative integer or p is not in [0, 1].

Type
Error
Returns:

A single sample, an array, or a multidimensional array of binomial numbers.

Type
number | Array
Example
binomial(10, 0.5)         // number of heads in 10 coin flips
binomial(1e9, 0.3, 5)     // five samples, O(log n) each

binomialBtpe()

BTPE for p <= 0.5. @private

Source:

binomialSample(n, p) → {number}

Binomial deviate with numpy's algorithms: inversion when min(p, 1 - p) * n <= 30 and the BTPE triangle-parallelogram-exponential rejection method (Kachitvichyanukul and Schmeiser, 1988) otherwise. Exact, O(1) expected time for any n.

Parameters:
Name Type Description
n number

Number of trials (non-negative integer).

p number

Success probability.

Source:
Returns:
Type
number

bytes(length) → {Uint8Array}

Returns length random bytes.

Parameters:
Name Type Description
length number

Number of bytes (non-negative integer).

Source:
Returns:
Type
Uint8Array

cauchy(median, gamma, sizeopt) → {number|Array}

Generates random samples from a Cauchy distribution.

Parameters:
Name Type Attributes Default Description
median number

Location parameter (median of the distribution).

gamma number

Scale parameter (dispersion of the distribution, must be > 0).

size number | Array | null <optional>
null

The shape of the output:

  • If null, a single value is returned.
  • If a number, a 1D array of the specified size is returned.
  • If an array (e.g., [m, n]), a multidimensional array of the specified shape is returned.
Source:
Throws:

Throws an error if:

  • The size parameter is not a number, an array or null.
  • The gamma parameter is not positive.
Type
Error
Returns:

A single value, an array, or a multidimensional array of random values from the Cauchy distribution.

Type
number | Array

chisquare(k, sizeopt) → {number|Array}

Generates random numbers following a Chi-squared distribution with k degrees of freedom. Non-integer degrees of freedom are supported (as numpy).

Parameters:
Name Type Attributes Default Description
k number

Degrees of freedom (> 0).

size number | Array | null <optional>
null

The shape of the output:

  • If null, a single number is returned.
  • If a number, a 1D array of the specified size is returned.
  • If an array (e.g., [m, n]), a multidimensional array of the specified shape is returned.
Source:
Throws:

If the degrees of freedom are not a positive number.

Type
Error
Returns:

A random number or an array of random numbers from the Chi-squared distribution.

Type
number | Array

choice(array, weightsopt, sizeopt, replaceopt) → {*|Array}

Generates a random sample from a given array or from range(n), as numpy's choice.

Parameters:
Name Type Attributes Default Description
array Array | number

Population, or an integer n meaning [0, 1, ..., n-1].

weights Array.<number> | null <optional>
null

Non-negative weights (normalised automatically). Uniform when null.

size number | Array.<number> | null <optional>
null

Output shape. null returns a single element.

replace boolean <optional>
true

Whether the sample is with replacement.

Source:
Throws:

If the population is empty, the weights are invalid, or more elements than available are requested without replacement.

Type
Error
Returns:

The sampled element(s).

Type
* | Array
Example
choice(['a', 'b', 'c'])                    // one element
choice(['a', 'b'], [0.9, 0.1], 10)          // ten weighted draws
choice(52, null, 5, false)                 // five distinct cards out of 52
choice(['x', 'y', 'z'], [5, 1, 1], 2, false)  // weighted, without replacement

choleskyDecomposition(matrix, tolopt) → {Array.<Array.<number>>}

Computes the Cholesky decomposition of a symmetric positive semi-definite matrix.

Given a matrix A, returns a lower triangular matrix L such that A = L * L^T. Singular (positive semi-definite) matrices are accepted: pivots within a relative tolerance of zero produce zero columns, as needed for degenerate covariance matrices.

Parameters:
Name Type Attributes Default Description
matrix Array.<Array.<number>>

The symmetric positive (semi-)definite matrix to decompose.

tol number <optional>
1e-10

Relative tolerance used to decide that a pivot is zero.

Source:
Throws:

Will throw an error if the matrix is not square, not symmetric or not positive semi-definite.

References:

Returns:

Lower triangular matrix L.

Type
Array.<Array.<number>>

createGenerator(seedopt, bitGeneratoropt) → {Generator}

Creates a new Generator, numpy's default_rng equivalent.

Parameters:
Name Type Attributes Default Description
seed number | bigint | string | Array.<number> | BitGenerator <optional>

Seed, or an existing bit generator. Random when omitted.

bitGenerator string <optional>
'xoshiro128**'

Bit generator name: 'xoshiro128**', 'pcg32' or 'mt19937'.

Source:
Returns:
Type
Generator
Example
const rng = createGenerator(2024)               // xoshiro128**
const legacy = createGenerator(42, 'mt19937')   // numpy RandomState compatible streams
legacy.random()                                 // 0.3745401188473625

defaultRng(seedopt, bitGeneratoropt) → {Generator}

Alias of createGenerator, named after numpy's default_rng.

Parameters:
Name Type Attributes Default Description
seed number | bigint | string | Array.<number> | BitGenerator <optional>
bitGenerator string <optional>
'xoshiro128**'
Source:
Returns:
Type
Generator

dirichlet(alpha, sizeopt) → {Array.<number>|Array}

Draws samples from the Dirichlet distribution: random probability vectors.

Uses normalised gamma variates, switching to beta stick-breaking when every alpha is small (< 0.1) to avoid underflow. Unlike numpy, the remaining stick is tracked with the exactly computed beta complement, so components never collapse to 0 through rounding.

Parameters:
Name Type Attributes Default Description
alpha Array.<number>

Concentration parameters (each > 0).

size number | Array.<number> | null <optional>
null

Output shape (each element is a vector).

Source:
Returns:

Sample vector(s) summing to 1.

Type
Array.<number> | Array
Example
dirichlet([1, 1, 1])        // uniform on the 2-simplex

erfc(x) → {number}

Complementary error function, relative accuracy ~1e-15 (W. J. Cody, 1969).

Parameters:
Name Type Description
x number
Source:
Returns:
Type
number

exponential(lambdaopt, sizeopt) → {number|Array}

Generates random samples from an exponential distribution.

Parameters:
Name Type Attributes Default Description
lambda number <optional>
1

The rate parameter (must be > 0). Note: numpy uses the scale 1/lambda.

size number | Array | null <optional>
null

The shape of the output:

  • If null, a single value is returned.
  • If a number, a 1D array of the specified size is returned.
  • If an array (e.g., [m, n]), a multidimensional array of the specified shape is returned.
Source:
Throws:

Throws an error if:

  • The size parameter is not a number, an array or null.
  • The rate parameter lambda is not positive.
Type
Error
Returns:

A single value, an array, or a multidimensional array of random values from the exponential distribution.

Type
number | Array

f(dfnum, dfden, sizeopt) → {number|Array}

Draws samples from an F (Fisher–Snedecor) distribution.

Parameters:
Name Type Attributes Default Description
dfnum number

Degrees of freedom of the numerator (> 0).

dfden number

Degrees of freedom of the denominator (> 0).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

gamma(shape, scaleopt, sizeopt) → {number|Array}

Draws samples from a Gamma distribution with shape k and scale theta (mean k·theta, variance k·theta²). Exact Marsaglia–Tsang method.

Parameters:
Name Type Attributes Default Description
shape number

Shape k (>= 0).

scale number <optional>
1

Scale theta (>= 0).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array
Example
gamma(2, 3, 1000)   // shape 2, scale 3

geometric(p, sizeopt) → {number|Array}

Generates random numbers or arrays of random numbers for a geometric distribution: the number of trials needed to get the first success (support 1, 2, 3, ...), as numpy.

Parameters:
Name Type Attributes Default Description
p number

Probability of success in each trial, in (0, 1].

size number | Array | null <optional>
null

The shape of the output:

  • If null, a single number is returned.
  • If a number, a 1D array of the specified size is returned.
  • If an array (e.g., [m, n]), a multidimensional array of the specified shape is returned.
Source:
Throws:

If p is not in (0, 1].

Type
Error
Returns:

A single sample, an array, or a multidimensional array of geometric numbers.

Type
number | Array

getRandomGenerator() → {function}

Returns the current global random generator as a function returning doubles in [0, 1).

Source:
Returns:

The random number generator function.

Type
function

getState() → {Object}

Returns the state of the global generator, which can later be restored with setState.

Source:
Returns:

A JSON serialisable snapshot of the global generator.

Type
Object

gumbel(locopt, scaleopt, sizeopt) → {number|Array}

Draws samples from the Gumbel (type I extreme value) distribution.

Parameters:
Name Type Attributes Default Description
loc number <optional>
0

Location (mode).

scale number <optional>
1

Scale (>= 0).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

hypergeometric(ngood, nbad, nsample, sizeopt) → {number|Array}

Draws samples from a hypergeometric distribution: the number of good items when drawing nsample items without replacement from ngood good and nbad bad items. Exact for any population size.

Parameters:
Name Type Attributes Default Description
ngood number

Number of good items (non-negative integer).

nbad number

Number of bad items (non-negative integer).

nsample number

Number of items drawn, <= ngood + nbad.

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

hypergeometricSample(good, bad, n) → {number}

Hypergeometric deviate: number of good items in a sample of n drawn without replacement from good good and bad bad items. Exact inversion starting at the mode and searching outwards, O(standard deviation) expected time.

Parameters:
Name Type Description
good number
bad number
n number
Source:
Returns:
Type
number

integers(low, highopt, sizeopt, endpointopt) → {number|Array}

Returns random integers from low (inclusive) to high (exclusive), or to high inclusive if endpoint is true. numpy's Generator.integers. If high is null, values are drawn from [0, low).

Parameters:
Name Type Attributes Default Description
low number

Lowest integer (or the upper bound if high is null).

high number | null <optional>
null

Upper bound.

size number | Array | null <optional>
null

Output shape.

endpoint boolean <optional>
false

Include high.

Source:
Returns:
Type
number | Array
Example
integers(1, 6, 10, true)   // ten dice rolls in [1, 6]

inverseNormalCDF(p) → {number}

Inverse of the standard normal cumulative distribution function (quantile function).

Implements Wichura's algorithm AS 241 (PPND16), accurate to about 1 part in 10^16.

Reference: M. J. Wichura (1988), "Algorithm AS 241: The Percentage Points of the Normal Distribution", Applied Statistics 37(3), 477-484.

Parameters:
Name Type Description
p number

Probability in [0, 1].

Source:
Returns:

The value x such that P(Z <= x) = p for Z ~ N(0, 1). Returns -Infinity for p = 0, Infinity for p = 1 and NaN outside [0, 1].

Type
number

laplace(locopt, scaleopt, sizeopt) → {number|Array}

Draws samples from the Laplace (double exponential) distribution.

Parameters:
Name Type Attributes Default Description
loc number <optional>
0

Location (mean / median).

scale number <optional>
1

Scale (>= 0).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

logFactorial(k) → {number}

log(k!) for non-negative integers.

Parameters:
Name Type Description
k number
Source:
Returns:
Type
number

logGamma(x) → {number}

Natural logarithm of the gamma function (Lanczos approximation, g = 7, n = 9). Accurate to ~1e-15 for x > 0.

Parameters:
Name Type Description
x number
Source:
Returns:
Type
number

logistic(locopt, scaleopt, sizeopt) → {number|Array}

Draws samples from the logistic distribution.

Parameters:
Name Type Attributes Default Description
loc number <optional>
0

Location (mean).

scale number <optional>
1

Scale (>= 0).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

lognormal(locopt, scaleopt, sizeopt) → {number|Array}

Generates random numbers or arrays of random numbers for a log-normal distribution.

Parameters:
Name Type Attributes Default Description
loc number <optional>
0.0

The mean of the underlying normal distribution to sample. 0.0 by default.

scale number <optional>
1.0

The standard deviation of the underlying normal distribution to sample. 1.0 by default.

size number | Array | null <optional>
null

The shape of the output:

  • If null, a single number is returned.
  • If a number, a 1D array of the specified size is returned.
  • If an array (e.g., [m, n]), a multidimensional array of the specified shape is returned.
Source:
Throws:

Throws an error if:

  • The size parameter is not a number, an array or null.
  • The scale parameter is not positive.
Type
Error
Returns:

A single sample, an array, or a multidimensional array of log-normal distributed numbers.

Type
number | Array

logseries(p, sizeopt) → {number|Array}

Draws samples from a logarithmic series distribution (support 1, 2, ...), Kemp's algorithm.

Parameters:
Name Type Attributes Default Description
p number

Shape parameter in [0, 1).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

lomax(a, sizeopt) → {number|Array}

Draws samples from a Pareto II (Lomax) distribution with shape a, location 0 and scale 1. This is numpy's pareto(a).

Parameters:
Name Type Attributes Default Description
a number

Shape (> 0).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

mapShape2()

Picks element k of every [value, label] pair at depth depth. @private

Source:

mixture(generators, priors, sizeopt, optionsopt) → {*|Array|Object}

Draws samples from a finite mixture: each sample comes from component i with probability weights[i]. Components are zero-argument sampling functions or distribution objects such as stats.normal(0, 1); they may even be multivariate. Component selection is O(1) (alias method).

See stats.mixture for the density, cdf, quantiles and moments of a mixture.

Parameters:
Name Type Attributes Default Description
generators Array.<(function()|Object)>

The mixture components.

priors Array.<number>

Non-negative weights of the components (normalised automatically).

size number | Array.<number> | null <optional>
null

Output shape.

options Object <optional>
Properties
Name Type Attributes Default Description
returnLabels boolean <optional>
false

Also return the index of the component of each sample, as { values, labels }.

Source:
Throws:

If the components or weights are invalid, or a component throws when sampled.

Type
Error
Returns:

The samples (and labels).

Type
* | Array | Object
Example
// bimodal: 70% N(0, 1), 30% N(5, 2²)
mixture([stats.normal(0, 1), stats.normal(5, 2)], [0.7, 0.3], 1000)
mixture([() => exponential(1), () => uniform(2, 3)], [1, 3], 10)   // weights are normalised

// keep the component of each sample (e.g. to test clustering algorithms)
const { values, labels } = mixture([stats.normal(-2, 1), stats.normal(2, 1)], [0.5, 0.5], 100, { returnLabels: true })

multinomial(n, pvals, sizeopt) → {Array.<number>|Array}

Draws samples from a multinomial distribution: counts of each of k outcomes in n independent trials. Exact, using conditional binomials (O(k log n) per sample).

Parameters:
Name Type Attributes Default Description
n number

Number of trials (non-negative integer).

pvals Array.<number>

Probabilities of each outcome. They must sum to 1; as numpy, the last entry is implied (1 - sum of the others).

size number | Array.<number> | null <optional>
null

Output shape (each element is a vector of counts).

Source:
Returns:
Type
Array.<number> | Array
Example
multinomial(20, [1/6, 1/6, 1/6, 1/6, 1/6, 1/6])   // counts of each face in 20 dice throws

multivariate(means, covariance, noiseGenerator, sizeopt) → {Array.<number>|Array.<Array.<number>>|Array}

Generates random vectors x = μ + L z, where Σ = L Lᵀ is the Cholesky factorisation of the covariance matrix and z is a noise vector with independent zero-mean unit-variance components produced by noiseGenerator. The result has mean μ and covariance Σ whatever the noise distribution (normal noise gives the multivariate normal distribution).

Parameters:
Name Type Attributes Default Description
means Array.<number>

Array of means (μ) for each dimension.

covariance Array.<Array.<number>>

Covariance matrix (Σ). Must be symmetric positive semi-definite and match the length of means.

noiseGenerator function

Function receiving the dimension n and returning a noise vector of length n: noiseGenerator(n) → number[].

size number | Array.<number> | null <optional>
null

Output shape:

  • null: a single sample vector.
  • k: an array of k sample vectors.
  • [m, k]: a nested array of shape m x k of sample vectors.
Source:
Throws:

If the shapes are invalid, the covariance is not positive semi-definite, noiseGenerator(n) does not return a vector of length n or size is invalid.

Type
Error
Returns:

Sample vector(s).

Type
Array.<number> | Array.<Array.<number>> | Array

multivariateHypergeometric(colors, nsample, sizeopt) → {Array.<number>|Array}

Draws samples from a multivariate hypergeometric distribution: counts of each colour when drawing nsample items without replacement from an urn with colors[i] items of colour i. Exact (conditional hypergeometric draws).

Parameters:
Name Type Attributes Default Description
colors Array.<number>

Number of items of each colour (non-negative integers).

nsample number

Number of items drawn, <= sum(colors).

size number | Array.<number> | null <optional>
null

Output shape (each element is a vector of counts).

Source:
Returns:
Type
Array.<number> | Array

multivariateNormal(means, covariance, sizeopt) → {Array.<number>|Array.<Array.<number>>|Array}

Generates samples from a multivariate normal distribution using the provided means and covariance matrix.

This is a convenience wrapper around multivariate, using a standard normal generator for the noise.

Parameters:
Name Type Attributes Default Description
means Array.<number>

Array of means (μ) for each dimension.

covariance Array.<Array.<number>>

Covariance matrix (Σ). Must be symmetric positive semi-definite (singular matrices are allowed) and match the size of means.

size number | Array.<number> | null <optional>
null

Output shape: null returns one sample vector, k an array of k vectors and [m, k] a nested m x k array of vectors (as numpy).

Source:
Throws:

If the input shapes are invalid or the covariance matrix is not positive semi-definite.

Type
Error
Returns:

Sample vector(s).

Type
Array.<number> | Array.<Array.<number>> | Array

multivariateTriangular(means, covariance, alphaopt, sizeopt) → {Array.<number>|Array.<Array.<number>>|Array}

Generates samples from a multivariate triangular distribution (possibly asymmetric) with the given means and covariance matrix.

The noise components follow a triangular distribution standardised to mean 0 and variance 1, so the samples have exactly the requested mean and covariance.

Parameters:
Name Type Attributes Default Description
means Array.<number>

Array of means (μ) for each dimension.

covariance Array.<Array.<number>>

Covariance matrix (Σ). Must be symmetric positive semi-definite and match means.

alpha number <optional>
0

Asymmetry parameter in [-1, 1]:

  • 0 → symmetric
  • < 0 → skewed left
  • 0 → skewed right

size number | Array.<number> | null <optional>
null

Output shape: null returns one sample vector, k an array of k vectors and [m, k] a nested array of vectors.

Source:
Throws:

If alpha is not in [-1, 1] or the inputs are invalid.

Type
Error
Returns:

Sample vector(s).

Type
Array.<number> | Array.<Array.<number>> | Array

multivariateUniform(means, covariance, sizeopt) → {Array.<number>|Array.<Array.<number>>|Array}

Generates samples from a multivariate uniform distribution using the provided means and covariance matrix.

This is a convenience wrapper around multivariate, using a standard uniform generator for the noise.

Parameters:
Name Type Attributes Default Description
means Array.<number>

Array of means (μ) for each dimension.

covariance Array.<Array.<number>>

Covariance matrix (Σ). Must be symmetric positive semi-definite (singular matrices are allowed) and match the size of means.

size number | Array.<number> | null <optional>
null

Output shape: null returns one sample vector, k an array of k vectors and [m, k] a nested m x k array of vectors (as numpy).

Source:
Throws:

If the input shapes are invalid or the covariance matrix is not positive semi-definite.

Type
Error
Returns:

Sample vector(s).

Type
Array.<number> | Array.<Array.<number>> | Array

negativeBinomial(n, p, sizeopt) → {number|Array}

Draws samples from a negative binomial distribution: the number of failures before n successes with success probability p. n may be non-integer (Pólya distribution).

Parameters:
Name Type Attributes Default Description
n number

Number of successes (> 0).

p number

Success probability in (0, 1].

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

noncentralChisquare(df, nonc, sizeopt) → {number|Array}

Draws samples from a noncentral chi-square distribution.

Parameters:
Name Type Attributes Default Description
df number

Degrees of freedom (> 0).

nonc number

Non-centrality (>= 0).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

noncentralF(dfnum, dfden, nonc, sizeopt) → {number|Array}

Draws samples from a noncentral F distribution.

Parameters:
Name Type Attributes Default Description
dfnum number

Numerator degrees of freedom (> 0).

dfden number

Denominator degrees of freedom (> 0).

nonc number

Non-centrality (>= 0).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

normal(locopt, scaleopt, sizeopt) → {number|Array}

Generates random numbers or arrays of random numbers for a normal (Gaussian) distribution.

Samples are exact (Marsaglia polar method), not approximations.

Parameters:
Name Type Attributes Default Description
loc number <optional>
0.0

The mean of the normal distribution to sample. 0.0 by default.

scale number <optional>
1.0

The standard deviation of the normal distribution to sample. 1.0 by default.

size number | Array | null <optional>
null

The shape of the output:

  • If null, a single number is returned.
  • If a number, a 1D array of the specified size is returned.
  • If an array (e.g., [m, n]), a multidimensional array of the specified shape is returned.
Source:
Throws:

If scale is negative or the parameters are not numbers.

Type
Error
Returns:

A single sample, an array, or a multidimensional array of normal distributed numbers.

Type
number | Array
Example
normal()              // one N(0, 1) sample
normal(10, 2, [2, 3]) // 2x3 array of N(10, 2^2) samples

normalCDF(x) → {number}

Standard normal cumulative distribution function, computed through a high precision complementary error function (W. J. Cody's rational approximations).

Parameters:
Name Type Description
x number
Source:
Returns:

P(Z <= x) for Z ~ N(0, 1).

Type
number

pareto(alpha, xmopt, sizeopt) → {number|Array}

Generates random numbers or arrays of random numbers for a (type I) Pareto distribution with tail index alpha and minimum xm: P(X > x) = (xm / x)^alpha for x >= xm.

Note: numpy's pareto(a) is the Pareto II / Lomax distribution, available here as lomax. pareto(a, 1) - 1 has the same distribution as numpy's pareto(a).

Parameters:
Name Type Attributes Default Description
alpha number

The tail index (> 0).

xm number <optional>
1

The minimum value / scale (> 0).

size number | Array | null <optional>
null

The shape of the output:

  • If null, a single number is returned.
  • If a number, a 1D array of the specified size is returned.
  • If an array (e.g., [m, n]), a multidimensional array of the specified shape is returned.
Source:
Throws:

If alpha or xm are not positive numbers.

Type
Error
Returns:

A single sample, an array, or a multidimensional array of Pareto numbers.

Type
number | Array

permutation(x, axisopt) → {Array}

Generates a random permutation of an array or a range of integers. The input array is not modified (a copy is shuffled), as numpy.

Parameters:
Name Type Attributes Default Description
x Array | number

If an array, a shuffled copy is returned (nested arrays are permuted along the first axis). If a number n, a random permutation of [0, 1, ..., n-1].

axis number <optional>
0

Axis along which to permute nested arrays.

Source:
Returns:

A randomly permuted array.

Type
Array
Example
permutation(5)            // e.g. [3, 0, 4, 1, 2]
permutation(['a', 'b'])   // ['b', 'a'] or ['a', 'b']

permuted(x, axisopt, inPlaceopt) → {Array}

Randomly permutes a nested array along axis, shuffling every slice independently (numpy's Generator.permuted). With axis = null the array is flattened, shuffled and reshaped. Returns a new array unless inPlace is true.

Parameters:
Name Type Attributes Default Description
x Array

A (possibly nested, rectangular) array.

axis number | null <optional>
null

Axis along which to shuffle independently, or null.

inPlace boolean <optional>
false

Modify x instead of returning a copy.

Source:
Returns:
Type
Array
Example
permuted([[1, 2, 3], [4, 5, 6]], 1)   // each row shuffled independently

poisson(lambdaopt, sizeopt) → {number|Array}

Generates random numbers following a Poisson distribution with a given mean (lambda). The Poisson distribution models the number of events occurring in a fixed interval of time or space, given the events occur independently and at a constant average rate.

Sampling is exact for every lambda: the multiplication method for lambda < 10 and Hörmann's PTRS transformed rejection method otherwise (the same algorithms as numpy).

Parameters:
Name Type Attributes Default Description
lambda number <optional>
1

The mean number of occurrences (lambda >= 0).

size number | Array | null <optional>
null

The shape of the output:

  • If null, a single Poisson random value is returned.
  • If a number, a 1D array of the specified size is returned.
  • If an array (e.g., [m, n]), a multidimensional array of the specified shape is returned.
Source:
Throws:

If lambda is negative, not a number or larger than 1e15.

Type
Error
Returns:

A Poisson random number, an array of random numbers, or a multidimensional array.

Type
number | Array
Example
poisson(4)         // one sample with mean 4
poisson(1e6, 10)   // ten samples, still exact

poissonSample(lam) → {number}

Poisson deviate. Multiplication method for lam < 10, otherwise the PTRS transformed rejection method of Hörmann (1993), exact for any lam.

Parameters:
Name Type Description
lam number
Source:
Returns:
Type
number

power(a, sizeopt) → {number|Array}

Draws samples in [0, 1] from a power distribution with positive exponent a - 1 (density a·x^(a-1)).

Parameters:
Name Type Attributes Default Description
a number

Exponent parameter (> 0).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

randString(length, charsopt, sizeopt) → {string|Array}

Generates random strings or arrays of random strings.

Parameters:
Name Type Attributes Default Description
length number

The length of each generated string.

chars string <optional>
CHARS

The characters to use for generating the string.

size number | Array | null <optional>
null

The shape of the output:

  • If null, a single string is returned.
  • If a number, a 1D array of strings of that length is returned.
  • If an array (e.g. [m, n]), a multidimensional array of strings is returned.
Source:
Throws:

Throws if:

  • length is not a positive integer.
  • chars is not a non-empty string.
  • size is not null, a number, or an array of numbers.
Type
Error
Returns:

A single string or an array of random strings.

Type
string | Array

randint(low, highopt, sizeopt) → {number|Array}

Generates random integers or arrays of random integers within a specified range. Every integer in the range is exactly equally likely (rejection sampling, no modulo bias).

Parameters:
Name Type Attributes Default Description
low number

The inclusive lower bound of the random integers.

high number <optional>
null

The exclusive upper bound. If not provided, low is treated as high, and the range is [0, low).

size number | Array | null <optional>
null

The shape of the output:

  • If null, a single integer is returned.
  • If a number, a 1D array of the specified size is returned.
  • If an array (e.g., [m, n]), a multidimensional array of the specified shape is returned.
Source:
Throws:

If low is greater than or equal to high, bounds are not integers, or the range exceeds 2^53.

Type
Error
Returns:

A random integer, an array of random integers, or a multidimensional array.

Type
number | Array
Example
randint(6)            // integer in [0, 6)
randint(1, 7, 10)     // ten dice rolls

random(sizeopt) → {number|Array}

Returns uniformly distributed floats in the half-open interval [0, 1) (numpy's random / random_sample).

Parameters:
Name Type Attributes Default Description
size number | Array.<number> | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

randomLoggam(x) → {number}

log Gamma(x) for x > 0 with numpy's random_loggam (Stirling series), used where numpy uses it so that rejection decisions match numpy exactly.

Parameters:
Name Type Description
x number
Source:
Returns:
Type
number

rayleigh(scaleopt, sizeopt) → {number|Array}

Draws samples from the Rayleigh distribution.

Parameters:
Name Type Attributes Default Description
scale number <optional>
1

Scale / mode (>= 0).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

seed(seedValueopt, bitGeneratoropt) → {void}

Seeds the global random number generator used by all the module level functions.

Calling seed with the same value always reproduces the same sequence of samples.

Parameters:
Name Type Attributes Default Description
seedValue number | bigint | string | Array.<number> <optional>

Seed value. When omitted the generator is re-seeded from a secure entropy source (crypto.getRandomValues when available).

bitGenerator string <optional>
'xoshiro128**'

Bit generator: 'xoshiro128**' (default), 'pcg32' or 'mt19937'.

Source:
Throws:

If the seed is not a number, bigint, string or array of unsigned 32-bit integers.

Type
Error
Returns:
Type
void
Example
seed(42)
uniform()                // always the same value
seed(42, 'mt19937')
random()                 // 0.3745401188473625, same as np.random.RandomState(42).random_sample()

setRandomSource(fn) → {void}

Replaces the global source of randomness by a function returning uniform doubles in [0, 1), for example Math.random or a custom/secure generator. The state of such a source cannot be saved.

Parameters:
Name Type Description
fn function

Uniform [0, 1) generator.

Source:
Returns:
Type
void

setState(state) → {void}

Restores a state previously obtained with getState.

Parameters:
Name Type Description
state Object

Snapshot returned by getState.

Source:
Returns:
Type
void

shuffle(array, axisopt) → {Array}

Shuffles an array in place (Fisher-Yates) along the given axis and returns it.

For nested arrays only the order of the slices along axis changes; the same permutation is applied to every slice, so rows (axis 0) or columns (axis 1) move as a whole, as numpy.

Parameters:
Name Type Attributes Default Description
array Array

The array to shuffle in place.

axis number <optional>
0

Axis along which to shuffle.

Source:
Returns:

The same array, shuffled.

Type
Array
Example
shuffle([1, 2, 3, 4])
shuffle([[1, 2], [3, 4]], 1)   // swaps columns

standardCauchy(sizeopt) → {number|Array}

Draws samples from the standard Cauchy distribution (median 0, scale 1).

Parameters:
Name Type Attributes Default Description
size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

standardExponential() → {number}

Standard exponential deviate (rate 1): numpy's ziggurat, or inversion -log(1 - U) for the legacy bit generators (as RandomState.standard_exponential).

Source:
Returns:
Type
number

standardExponential(sizeopt) → {number|Array}

Draws samples from the standard exponential distribution (rate 1, mean 1).

Parameters:
Name Type Attributes Default Description
size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

standardGamma(a) → {number}

Standard gamma deviate with shape a (scale 1), numpy's algorithm: Marsaglia and Tsang (2000) for a > 1 and the Ahrens-Dieter GS rejection method for a < 1.

Parameters:
Name Type Description
a number

Shape (>= 0).

Source:
Returns:
Type
number

standardGamma(shape, sizeopt) → {number|Array}

Draws samples from a standard gamma distribution with the given shape (scale 1).

Parameters:
Name Type Attributes Default Description
shape number

Shape k (>= 0).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

standardNormal() → {number}

Standard normal deviate. Uses numpy's 256-layer ziggurat (bit-identical to default_rng(seed).standard_normal() with PCG64), or numpy's legacy polar method for MT19937 (bit-identical to RandomState(seed).standard_normal()).

Source:
Returns:
Type
number

standardNormal(sizeopt) → {number|Array}

Draws samples from the standard normal distribution N(0, 1). Equivalent to numpy's standard_normal.

Parameters:
Name Type Attributes Default Description
size number | Array | null <optional>
null

Output shape.

Source:
Returns:

Standard normal samples.

Type
number | Array

standardT(df, sizeopt) → {number|Array}

Draws samples from Student's t distribution with df degrees of freedom.

Parameters:
Name Type Attributes Default Description
df number

Degrees of freedom (> 0).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

studentT(df, locopt, scaleopt, sizeopt) → {number|Array}

Draws samples from Student's t distribution with location and scale.

Parameters:
Name Type Attributes Default Description
df number

Degrees of freedom (> 0).

loc number <optional>
0

Location.

scale number <optional>
1

Scale (> 0).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

triangular(a, b, c, sizeopt) → {number|Array}

Generates random numbers or arrays of random numbers for a triangular distribution.

Note the parameter order (low, high, mode) differs from numpy's triangular(left, mode, right).

Parameters:
Name Type Attributes Default Description
a number

Lower limit.

b number

Upper limit.

c number

Mode, with a <= c <= b.

size number | Array | null <optional>
null

The shape of the output:

  • If null, a single number is returned.
  • If a number, a 1D array of the specified size is returned.
  • If an array (e.g., [m, n]), a multidimensional array of the specified shape is returned.
Source:
Throws:

If parameters a, b, c do not fulfill a <= c <= b and a < b.

Type
Error
Returns:

A single sample, an array, or a multidimensional array of triangular distributed numbers.

Type
number | Array

uniform(lowopt, highopt, sizeopt) → {number|Array}

Generates random floats or arrays of random floats within a specified range.

Parameters:
Name Type Attributes Default Description
low number <optional>
0.0

The lower bound. 0.0 by default.

high number <optional>
1.0

The upper bound. 1.0 by default.

size number | Array | null <optional>
null

The shape of the output:

  • If null, a single float is returned.
  • If a number, a 1D array of the specified size is returned.
  • If an array (e.g., [m, n]), a multidimensional array of the specified shape is returned.
Source:
Throws:

If low is greater than or equal to high.

Type
Error
Returns:

A random float, an array of random float, or a multidimensional array.

Type
number | Array

vonmises(mu, kappa, sizeopt) → {number|Array}

Draws samples from the von Mises (circular normal) distribution on [-pi, pi] using the Best–Fisher algorithm.

Parameters:
Name Type Attributes Default Description
mu number

Mode / center of the distribution.

kappa number

Concentration (>= 0). 0 gives the uniform distribution on the circle.

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

wald(mean, scale, sizeopt) → {number|Array}

Draws samples from a Wald (inverse Gaussian) distribution.

Parameters:
Name Type Attributes Default Description
mean number

Distribution mean (> 0).

scale number

Shape parameter lambda (> 0).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

weibull(a, sizeopt) → {number|Array}

Draws samples from a Weibull distribution with shape a and scale 1 (numpy's weibull). Multiply by a scale lambda to obtain the two-parameter Weibull.

Parameters:
Name Type Attributes Default Description
a number

Shape (>= 0).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array

zipf(a, sizeopt) → {number|Array}

Draws samples from a Zipf (zeta) distribution with exponent a > 1 (support 1, 2, ...), Devroye's rejection algorithm as used by numpy.

Parameters:
Name Type Attributes Default Description
a number

Exponent (> 1).

size number | Array | null <optional>
null

Output shape.

Source:
Returns:
Type
number | Array