Skip to main content
The Generator class is the recommended way to generate random numbers. It uses the PCG64 bit generator with SeedSequence initialization, exactly matching NumPy’s numpy.random.default_rng().

default_rng

Create a new Generator instance backed by PCG64.
function default_rng(seedValue?: number): Generator
NameTypeDefaultDescription
seedValuenumberundefinedOptional seed value. If omitted, a random seed is used.
Returns: Generator — A new random number generator.
import * as np from 'numpy-ts';

const rng = np.random.default_rng(42);
console.log(rng.random()); // Reproducible across runs

Generator Class

The Generator class provides the following methods. Each method mirrors the corresponding legacy np.random.* function but uses the PCG64 bit generator.

Generator.random

Generate random floats in [0.0, 1.0).
class Generator {
  random(size?: number | number[]): NDArray | number;
}
NameTypeDefaultDescription
sizenumber | number[]undefinedOutput shape. If omitted, returns a single float.
Returns: NDArray | number — Uniform random values in [0, 1).
import * as np from 'numpy-ts';

const rng = np.random.default_rng(0);
const a = rng.random([3, 3]);

Generator.integers

Return random integers from low (inclusive) to high (exclusive).
class Generator {
  integers(low: number, high?: number, size?: number | number[]): NDArray | number;
}
NameTypeDefaultDescription
lownumberLowest integer (inclusive). If high is omitted, range is [0, low).
highnumberundefinedUpper bound (exclusive).
sizenumber | number[]undefinedOutput shape.
Returns: NDArray | number — Random integers.
import * as np from 'numpy-ts';

const rng = np.random.default_rng(42);
const dice = rng.integers(1, 7, [10]);

Generator.uniform

Draw samples from a uniform distribution over [low, high).
class Generator {
  uniform(low?: number, high?: number, size?: number | number[]): NDArray | number;
}
NameTypeDefaultDescription
lownumber0Lower boundary (inclusive).
highnumber1Upper boundary (exclusive).
sizenumber | number[]undefinedOutput shape.
Returns: NDArray | number — Uniform samples.

Generator.normal

Draw samples from a normal (Gaussian) distribution.
class Generator {
  normal(loc?: number, scale?: number, size?: number | number[]): NDArray | number;
}
NameTypeDefaultDescription
locnumber0Mean of the distribution.
scalenumber1Standard deviation.
sizenumber | number[]undefinedOutput shape.
Returns: NDArray | number — Normal samples.
import * as np from 'numpy-ts';

const rng = np.random.default_rng(0);
const samples = rng.normal(100, 15, [1000]);

Generator.standard_normal

Draw samples from the standard normal distribution (mean=0, std=1).
class Generator {
  standard_normal(size?: number | number[]): NDArray | number;
}
NameTypeDefaultDescription
sizenumber | number[]undefinedOutput shape.
Returns: NDArray | number — Standard normal samples.

Generator.exponential

Draw samples from an exponential distribution.
class Generator {
  exponential(scale?: number, size?: number | number[]): NDArray | number;
}
NameTypeDefaultDescription
scalenumber1Scale parameter (beta = 1/lambda).
sizenumber | number[]undefinedOutput shape.
Returns: NDArray | number — Exponential samples.

Generator.poisson

Draw samples from a Poisson distribution.
class Generator {
  poisson(lam?: number, size?: number | number[]): NDArray | number;
}
NameTypeDefaultDescription
lamnumber1Expected number of events (lambda).
sizenumber | number[]undefinedOutput shape.
Returns: NDArray | number — Poisson samples.

Generator.binomial

Draw samples from a binomial distribution.
class Generator {
  binomial(n: number, p: number, size?: number | number[]): NDArray | number;
}
NameTypeDefaultDescription
nnumberNumber of trials.
pnumberProbability of success.
sizenumber | number[]undefinedOutput shape.
Returns: NDArray | number — Binomial samples.

Generator.choice

Randomly select elements from an array or range.
class Generator {
  choice(
    a: number | ArrayLike,
    size?: number | number[],
    replace?: boolean,
    p?: ArrayLike
  ): NDArray | number;
}
NameTypeDefaultDescription
anumber | ArrayLikeIf an integer, samples from arange(a). If an array, samples from its elements.
sizenumber | number[]undefinedOutput shape. If omitted, returns a single value.
replacebooleantrueWhether to sample with replacement.
pArrayLikeundefinedProbabilities for each element. Must sum to 1.
Returns: NDArray | number — Random sample(s).
import * as np from 'numpy-ts';

const rng = np.random.default_rng(42);

// Choose 5 random elements from [0, 10)
const a = rng.choice(10, 5);

// Weighted sampling without replacement
const b = rng.choice(np.array([10, 20, 30, 40]), 2, false, [0.1, 0.2, 0.3, 0.4]);

Generator.permutation

Return a randomly permuted copy of an array, or a permuted range.
class Generator {
  permutation(x: number | ArrayLike): NDArray;
}
NameTypeDefaultDescription
xnumber | ArrayLikeIf an integer, permute arange(x). If an array, return a shuffled copy.
Returns: NDArray — Permuted array.
import * as np from 'numpy-ts';

const rng = np.random.default_rng(0);

const perm = rng.permutation(10);
// array([...])  a random ordering of 0..9

Generator.shuffle

Shuffle an array in-place along the first axis.
class Generator {
  shuffle(x: NDArray): void;
}
NameTypeDefaultDescription
xNDArrayArray to be shuffled. Modified in-place.
Returns: void
import * as np from 'numpy-ts';

const rng = np.random.default_rng(0);
const arr = np.arange(10);
rng.shuffle(arr);
// arr is now shuffled in-place

Full Example

import * as np from 'numpy-ts';

// Create a seeded generator
const rng = np.random.default_rng(12345);

// Generate training data with noise
const x = rng.uniform(-5, 5, [100]);
const noise = rng.normal(0, 0.5, [100]);

// Random class labels
const labels = rng.integers(0, 3, [100]);

// Weighted random selection
const indices = rng.choice(100, 20, false);