Documentation Index
Fetch the complete documentation index at: https://numpyts.dev/llms.txt
Use this file to discover all available pages before exploring further.
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
| Name | Type | Default | Description |
|---|
seedValue | number | undefined | Optional 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;
}
| Name | Type | Default | Description |
|---|
size | number | number[] | undefined | Output 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;
}
| Name | Type | Default | Description |
|---|
low | number | — | Lowest integer (inclusive). If high is omitted, range is [0, low). |
high | number | undefined | Upper bound (exclusive). |
size | number | number[] | undefined | Output 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]);
Draw samples from a uniform distribution over [low, high).
class Generator {
uniform(low?: number, high?: number, size?: number | number[]): NDArray | number;
}
| Name | Type | Default | Description |
|---|
low | number | 0 | Lower boundary (inclusive). |
high | number | 1 | Upper boundary (exclusive). |
size | number | number[] | undefined | Output 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;
}
| Name | Type | Default | Description |
|---|
loc | number | 0 | Mean of the distribution. |
scale | number | 1 | Standard deviation. |
size | number | number[] | undefined | Output 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;
}
| Name | Type | Default | Description |
|---|
size | number | number[] | undefined | Output 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;
}
| Name | Type | Default | Description |
|---|
scale | number | 1 | Scale parameter (beta = 1/lambda). |
size | number | number[] | undefined | Output shape. |
Returns: NDArray | number — Exponential samples.
Generator.poisson
Draw samples from a Poisson distribution.
class Generator {
poisson(lam?: number, size?: number | number[]): NDArray | number;
}
| Name | Type | Default | Description |
|---|
lam | number | 1 | Expected number of events (lambda). |
size | number | number[] | undefined | Output 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;
}
| Name | Type | Default | Description |
|---|
n | number | — | Number of trials. |
p | number | — | Probability of success. |
size | number | number[] | undefined | Output 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;
}
| Name | Type | Default | Description |
|---|
a | number | ArrayLike | — | If an integer, samples from arange(a). If an array, samples from its elements. |
size | number | number[] | undefined | Output shape. If omitted, returns a single value. |
replace | boolean | true | Whether to sample with replacement. |
p | ArrayLike | undefined | Probabilities 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;
}
| Name | Type | Default | Description |
|---|
x | number | ArrayLike | — | If 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;
}
| Name | Type | Default | Description |
|---|
x | NDArray | — | Array 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);