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
Generatorclass created vianp.random.default_rng(). Uses PCG64 with SeedSequence, matching NumPy’s recommended modern approach.
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 toset_state to restore the RNG to this exact point.
{ 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 withname and state properties.
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
When using the legacy API with
np.random.seed(), uniform and normal outputs match NumPy exactly for the same seed. Other distributions use the same algorithms as NumPy and produce statistically equivalent results, though individual values may differ due to floating-point ordering.default_rng and Generator
The modern API usesdefault_rng to create a Generator instance backed by PCG64. See the Generator API page for full details.