Skip to main content
The np.random namespace provides NumPy-compatible random number generation with two APIs:
  • Legacy API — Global functions like np.random.seed(), np.random.rand(), np.random.normal(), etc. Uses MT19937 (Mersenne Twister), matching NumPy’s legacy random module.
  • Modern API — The Generator class created via np.random.default_rng(). Uses PCG64 with SeedSequence, matching NumPy’s recommended modern approach.
Both APIs produce statistically correct distributions and can be seeded for reproducibility.

Seeding and State

seed

Set the global seed for the legacy random number generator.
Returns: void

get_state

Get the internal state of the legacy MT19937 random number generator. The returned object can be passed to set_state to restore the RNG to this exact point.
Returns: { mt: number[]; mti: number } — Object containing the MT19937 state array and index.

set_state

Restore the internal state of the legacy random number generator from a previously saved state object.
Returns: void

get_bit_generator

Get the current bit generator object. Returns an object with name and state properties.
Returns: BitGenerator — The current bit generator descriptor.

set_bit_generator

Set the bit generator used by the legacy random functions.
Returns: void

Reproducibility

numpy-ts implements the same algorithms as NumPy for both the legacy and modern APIs:

NumPy Compatibility

As of v1.2.0, all distributions match NumPy exactly for the same seed, across both the legacy (np.random.seed()) and modern (np.random.default_rng()) APIs. The legacy API uses MT19937 implemented in Zig/WASM; the modern API uses PCG64 in Zig/WASM.

default_rng and Generator

The modern API uses default_rng to create a Generator instance backed by PCG64. See the Generator API page for full details.