Skip to main content
All functions on this page are accessed via the np.random namespace and use the global MT19937 generator. Seed with np.random.seed() for reproducible results.

random

Generate random floats in the half-open interval [0.0, 1.0).
function random(size?: number | number[]): NDArray | number
NameTypeDefaultDescription
sizenumber | number[]undefinedOutput shape. If omitted, returns a single float.
Returns: NDArray | number — Random values in [0, 1).
import * as np from 'numpy-ts';

np.random.seed(42);

// Single value
const x = np.random.random();

// 1-D array
const a = np.random.random(5);
// array([...])  shape [5]

// 2-D array
const b = np.random.random([3, 4]);
// array([...])  shape [3, 4]

rand

Random values in a given shape. Variadic form of random — pass dimensions as separate arguments.
function rand(...shape: number[]): NDArray | number
NameTypeDefaultDescription
...shapenumber[]Dimensions of the returned array. If no arguments, returns a single float.
Returns: NDArray | number — Random values in [0, 1).
import * as np from 'numpy-ts';

const a = np.random.rand(2, 3);
// array([...])  shape [2, 3]

randn

Return samples from the standard normal distribution (mean=0, std=1). Variadic shape, like rand.
function randn(...shape: number[]): NDArray | number
NameTypeDefaultDescription
...shapenumber[]Dimensions of the returned array. If no arguments, returns a single float.
Returns: NDArray | number — Samples from the standard normal distribution.
import * as np from 'numpy-ts';

const a = np.random.randn(1000);
// 1000 standard-normal samples

randint

Return random integers from low (inclusive) to high (exclusive).
function randint(
  low: number,
  high?: number | null,
  size?: number | number[],
  dtype?: DType
): NDArray | number
NameTypeDefaultDescription
lownumberLowest integer (inclusive). If high is omitted, this is treated as the upper bound and low defaults to 0.
highnumber | nullnullOne above the highest integer (exclusive).
sizenumber | number[]undefinedOutput shape. If omitted, returns a single integer.
dtypeDType'int64'Desired output dtype.
Returns: NDArray | number — Random integers in [low, high).
import * as np from 'numpy-ts';

// 0 to 9
const a = np.random.randint(10);

// 5 to 14, shape [2, 3]
const b = np.random.randint(5, 15, [2, 3]);

random_sample

Alias for random(). Return random floats in [0.0, 1.0).
function random_sample(size?: number | number[]): NDArray | number
NameTypeDefaultDescription
sizenumber | number[]undefinedOutput shape.
Returns: NDArray | number — Random values in [0, 1).

ranf

Alias for random(). Return random floats in [0.0, 1.0).
function ranf(size?: number | number[]): NDArray | number
NameTypeDefaultDescription
sizenumber | number[]undefinedOutput shape.
Returns: NDArray | number — Random values in [0, 1).

sample

Alias for random(). Return random floats in [0.0, 1.0).
function sample(size?: number | number[]): NDArray | number
NameTypeDefaultDescription
sizenumber | number[]undefinedOutput shape.
Returns: NDArray | number — Random values in [0, 1).

random_integers

Deprecated. Use randint instead.
Return random integers between low and high, inclusive on both ends.
function random_integers(
  low: number,
  high?: number,
  size?: number | number[]
): NDArray | number
NameTypeDefaultDescription
lownumberLowest integer. If high is omitted, range is [1, low].
highnumberundefinedHighest integer (inclusive).
sizenumber | number[]undefinedOutput shape.
Returns: NDArray | number — Random integers in [low, high].
import * as np from 'numpy-ts';

// Random integers from 1 to 6 (inclusive), like a die roll
const die = np.random.random_integers(1, 6, [10]);

bytes

Return random bytes as a Uint8Array.
function bytes(length: number): Uint8Array
NameTypeDefaultDescription
lengthnumberNumber of random bytes to generate.
Returns: Uint8Array — Array of random bytes.
import * as np from 'numpy-ts';

const b = np.random.bytes(16);
// Uint8Array of 16 random bytes

choice

Generate a random sample from a given 1-D array or from range(a) when a is an integer.
function choice(
  a: number | ArrayLike,
  size?: number | number[],
  replace?: boolean,
  p?: ArrayLike
): NDArray | number
NameTypeDefaultDescription
anumber | ArrayLikeIf number, sample from 0..a-1; otherwise sample from the provided values.
sizenumber | number[]undefinedOutput shape.
replacebooleantrueWhether sampling is with replacement.
pArrayLikeundefinedOptional probabilities for each entry in a.

permutation

Randomly permute a sequence or return a permuted range.
function permutation(x: number | ArrayLike): NDArray
NameTypeDefaultDescription
xnumber | ArrayLikeIf number, permute 0..x-1; otherwise permute the input values.

shuffle

Shuffle an array in place along the first axis.
function shuffle(x: ArrayLike): void
NameTypeDefaultDescription
xArrayLikeArray to shuffle in place.