Global

Methods

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

Generates random floats or arrays of random floats given a random number generator function and a size parameter.

Parameters:
Name Type Attributes Default Description
generator function

The random number generator function. It should return a random float between 0 (inclusive) and 1 (exclusive) on each call.

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:

Throws an error if the size parameter is not a number, an array, or null.

Type
Error
Returns:

A single random float, an array of random floats, or a multidimensional array depending on the value of the size parameter:

  • null: A single random float (e.g., 0.532).
  • number: A 1D array of random floats (e.g., [0.1, 0.4, 0.8]).
  • Array: A multidimensional array of random floats (e.g., [[0.1, 0.3], [0.4, 0.8]]).
Type
number | Array

binarySearch(array, value) → {number}

Performs binary search on a sorted array of floating-point numbers. Returns the index of the largest element that is smaller than the search value. If no such element exists, returns -1.

Parameters:
Name Type Description
array Array.<number>

The sorted array of floating-point numbers.

value number

The value to search for.

Source:
Returns:

The index of the largest element smaller than the search value.

Type
number

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

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

Parameters:
Name Type Attributes Default Description
n number

Number of trials.

p number

Probability of success in each trial.

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:
Returns:

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

Type
number | Array

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.

Parameters:
Name Type Attributes Default Description
k number

Degrees of freedom (must be a positive integer).

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 degrees of freedom parameter k is not a positive integer.
Type
Error
Returns:

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

Type
number | Array

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

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

Given a matrix A, returns a lower triangular matrix L such that: A = L * L^T

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

The symmetric, positive-definite matrix to decompose.

Source:
Throws:

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

References:

Returns:

Lower triangular matrix L.

Type
Array.<Array.<number>>

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

Generates random samples from an exponential distribution.

Parameters:
Name Type Attributes Default Description
lambda number

The rate parameter (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 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

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

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

Parameters:
Name Type Attributes Default Description
p number

Probability of success in each trial.

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:
Returns:

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

Type
number | Array

getRandomGenerator() → {function}

Returns the current random generator function.

Source:
Returns:

The random number generator function.

Type
function

inverseNormalCDF(value)

Computes the inverse cumulative distribution function of a normal distribution.

Parameters:
Name Type Description
value number

The point to evaluate.

Source:

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

mixture(generatorsopt, priorsopt, sizeopt) → {number|Array}

Generates random numbers or arrays of random numbers for a mixture of given distributions.

Parameters:
Name Type Attributes Default Description
generators Array <optional>

The list of generators corresponding to each distribution.

priors number <optional>

The list of priors, one per distribution, must sum to 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 any of the generators raise an error on sampling.

Type
Error
Returns:

A single sample, an array, or a multidimensional array of numbers accoirding to the mixture of the distributions provided.

Type
number | Array

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

Generates random samples from a multivariate normal distribution.

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

Array of means (μ) for each dimension.

covariance Array.<Array.<number>>

Covariance matrix (Σ).

size number <optional>
1

Number of samples to generate.

Source:
Throws:

Throws an error if:

  • Means or covariances are not arrays of correct shape.
  • Covariance is not positive definite
  • The parameter size is not a positive integer.

References:

Type
Error
Returns:

An array of samples, each sample is an array of length equal to means.length.

Type
Array.<Array.<number>>

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

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

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 less or equal to 0.0.

Type
Error
Returns:

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

Type
number | Array

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

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

Parameters:
Name Type Attributes Default Description
alpha number

The tail index.

xm number

The minimum.

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 less or equal to 0.0.

Type
Error
Returns:

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

Type
number | Array

pcg64(seed) → {function}

PCG64 Random Number Generator

A JavaScript implementation of the Permuted Congruential Generator (PCG). This algorithm was introduced by Melissa E. O'Neill in her paper: "PCG: A Family of Simple Fast Space-Efficient Statistically Good Algorithms for Random Number Generation" (2014).

Reference:

  • Paper: https://www.pcg-random.org/pdf/toms-oneill-pcg-family-v1.02.pdf
  • Website: https://www.pcg-random.org/

Constants used (multiplier and increment) are based on the PCG64 default configuration:

  • Multiplier: 6364136223846793005 (prime relative to 2^64, ensures full period).
  • Increment: 1442695040888963407 (odd, ensures proper state transitions).

This implementation generates 64-bit random numbers and maps them to the range [0, 1).

Parameters:
Name Type Description
seed number | bigint

The initial seed for the random generator.

Source:
Returns:
  • A function that generates random numbers in [0, 1).
Type
function

permutation(x) → {Array}

Generates a random permutation of an array or a range of integers.

Parameters:
Name Type Description
x Array | number

If an array, it is shuffled and returned. If a number n, returns a random permutation of integers [0, 1, ..., n-1].

Source:
Returns:

A randomly permuted array.

Type
Array

poisson(lambda, 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.

Algorithm: Knuth's method is used for generating Poisson-distributed random numbers. For lambda <= 30, Knuth's algorithm is efficient and accurate. For larger lambda the normal approximation is used.

Parameters:
Name Type Attributes Default Description
lambda number

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 not a positive number.

Type
Error
Returns:

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

Type
number | Array

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

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

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.

Type
Error
Returns:

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

Type
number | Array

seed(seedValue) → {void}

Seeds the random number generator with a specified value.

Parameters:
Name Type Description
seedValue number

Value to initialize the random generator.

Source:
Throws:

Throws an error if the seedValue parameter is not a number.

Type
Error
Returns:
Type
void

shuffle(array) → {Array}

Shuffles the elements of an array along the first axis in-place.

For N-dimensional arrays, this function only shuffles the order of the top-level sub-arrays. The content and order within each sub-array remain unchanged.

Parameters:
Name Type Description
array Array

The array to shuffle. Must be N-dimensional.

Source:
Returns:

A new shuffled array if a deep copy is required, otherwise in-place modification.

Type
Array

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

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

Parameters:
Name Type Attributes Default Description
a number

Lower limit.

b number

Upper limit.

c number

Mode.

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.

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