Cyforanumera
npm

API reference

Random — bit generators

Low-level bit generators that back np.random.Generator. All bit generators implement random(), getState() and setState(). Prefer np.random.defaultRng() for everyday use.

NameSummary
np.SeedSequenceHashes seed entropy into a reproducible pool for initialising bit generators.
np.BitGeneratorNamespace of available P13 bit-generator constructors.
np.bit_generatorThe name of the underlying bit generator type (read-only property).
np.spawnReturn n independent Generator children seeded from OS entropy.
np.PCG64DXSMPCG64-DXSM bit generator — a variant of PCG64 with the DXSM output function.
np.PhiloxPhilox 4×64-10 counter-based bit generator.
np.SFC64SFC64 (Small Fast Counting) bit generator.

np.SeedSequence

#
new np.random.SeedSequence(entropy?, options?)

Hashes seed entropy into a reproducible pool for initialising bit generators. entropy can be an integer or array of integers; omit for OS-level randomness.

Parameters

[entropy]number | bigint | readonly (number|bigint)[]
Seed entropy; omit for non-reproducible randomness.
[options.spawnKey]readonly number[]
Spawn key for child sequences.
[options.poolSize]number
Size of the hashed entropy pool (>= 4). Default 4.

Returns

SeedSequence

Example

TypeScript
const ss = new np.random.SeedSequence(42);
ss.entropy;    // => 42
ss.poolSize;   // => 4
const [c] = ss.spawn(1);
c.spawnKey;    // => [0]
TypeScript declaration
new np.random.SeedSequence(entropy?: SeedLike | null | undefined, options?: SeedSequenceOptions | undefined)

np.BitGenerator

#
new np.random.BitGenerator(bitGenerator?)

Namespace of available P13 bit-generator constructors. Use individual constructors (e.g. PCG64, MT19937) or np.random.defaultRng() instead.

Returns

BitGenerator

Example

TypeScript
const bg = new np.random.PCG64(new np.random.SeedSequence(0));
typeof bg.random(); // => "number"
TypeScript declaration
new np.random.BitGenerator()

np.bit_generator

#
rng.bit_generator

The name of the underlying bit generator type (read-only property). Always "PCG64" for generators created by defaultRng. Equivalent to numpy.random.Generator.bit_generator.

Returns

string

Example

TypeScript
const rng = np.random.defaultRng(42);
rng.bit_generator; // => "PCG64"
TypeScript declaration
rng.bit_generator: string

np.spawn

#
rng.spawn(n)

Return n independent Generator children seeded from OS entropy. Each child is an independent PCG64-backed Generator. Equivalent to numpy.random.Generator.spawn.

Parameters

nnumber
Number of child generators to create.

Returns

Generator[]

Example

TypeScript
const rng = np.random.defaultRng(42);
const children = rng.spawn(3);
children.length; // => 3
TypeScript declaration
rng.spawn(n: number): Generator[]

np.PCG64DXSM

#
new np.random.PCG64DXSM(seedSequence)

PCG64-DXSM bit generator — a variant of PCG64 with the DXSM output function. Preferred over PCG64 for new code in NumPy >= 1.17.

Parameters

seedSequenceSeedSequence
Seed sequence to initialise the generator state.

Returns

PCG64DXSM

Example

TypeScript
const bg = new np.random.PCG64DXSM(new np.random.SeedSequence(1));
const v = bg.random();
v >= 0 && v < 1; // => true
TypeScript declaration
new np.random.PCG64DXSM(seed?: SeedLike | SeedSequence | null | undefined)

np.Philox

#
new np.random.Philox(seedSequence)

Philox 4×64-10 counter-based bit generator. Suitable for parallel random-number generation.

Parameters

seedSequenceSeedSequence
Seed sequence to initialise the generator state.

Returns

Philox

Example

TypeScript
const bg = new np.random.Philox(new np.random.SeedSequence(7));
const v = bg.random();
v >= 0 && v < 1; // => true
TypeScript declaration
new np.random.Philox(seed?: SeedLike | SeedSequence | null | undefined)

np.SFC64

#
new np.random.SFC64(seedSequence)

SFC64 (Small Fast Counting) bit generator. Fast, non-cryptographic, excellent statistical properties.

Parameters

seedSequenceSeedSequence
Seed sequence to initialise the generator state.

Returns

SFC64

Example

TypeScript
const bg = new np.random.SFC64(new np.random.SeedSequence(3));
const v = bg.random();
v >= 0 && v < 1; // => true
TypeScript declaration
new np.random.SFC64(seed?: SeedLike | SeedSequence | null | undefined)