Random sampling (numpy.random) — NumPy v2.0 Manual (2024)

Quick start#

The numpy.random module implements pseudo-random number generators(PRNGs or RNGs, for short) with the ability to draw samples from a variety ofprobability distributions. In general, users will create a Generator instancewith default_rng and call the various methods on it to obtain samples fromdifferent distributions.

>>> import numpy as np>>> rng = np.random.default_rng()# Generate one random float uniformly distributed over the range [0, 1)>>> rng.random() 0.06369197489564249 # may vary# Generate an array of 10 numbers according to a unit Gaussian distribution.>>> rng.standard_normal(10) array([-0.31018314, -1.8922078 , -0.3628523 , -0.63526532, 0.43181166, # may vary 0.51640373, 1.25693945, 0.07779185, 0.84090247, -2.13406828])# Generate an array of 5 integers uniformly over the range [0, 10).>>> rng.integers(low=0, high=10, size=5) array([8, 7, 6, 2, 0]) # may vary

Our RNGs are deterministic sequences and can be reproduced by specifying a seed integer toderive its initial state. By default, with no seed provided, default_rng willseed the RNG from nondeterministic data from the operating system and thereforegenerate different numbers each time. The pseudo-random sequences will beindependent for all practical purposes, at least those purposes for which ourpseudo-randomness was good for in the first place.

>>> rng1 = np.random.default_rng()>>> rng1.random() 0.6596288841243357 # may vary>>> rng2 = np.random.default_rng()>>> rng2.random() 0.11885628817151628 # may vary

Warning

The pseudo-random number generators implemented in this module are designedfor statistical modeling and simulation. They are not suitable for securityor cryptographic purposes. See the secrets module from thestandard library for such use cases.

Seeds should be large positive integers. default_rng can take positiveintegers of any size. We recommend using very large, unique numbers to ensurethat your seed is different from anyone else’s. This is good practice to ensurethat your results are statistically independent from theirs unless you areintentionally trying to reproduce their result. A convenient way to getsuch a seed number is to use secrets.randbits to get anarbitrary 128-bit integer.

>>> import secrets>>> import numpy as np>>> secrets.randbits(128) 122807528840384100672342137672332424406 # may vary>>> rng1 = np.random.default_rng(122807528840384100672342137672332424406)>>> rng1.random()0.5363922081269535>>> rng2 = np.random.default_rng(122807528840384100672342137672332424406)>>> rng2.random()0.5363922081269535

See the documentation on default_rng and SeedSequence for more advancedoptions for controlling the seed in specialized scenarios.

Generator and its associated infrastructure was introduced in NumPy version1.17.0. There is still a lot of code that uses the older RandomState and thefunctions in numpy.random. While there are no plans to remove them at thistime, we do recommend transitioning to Generator as you can. The algorithmsare faster, more flexible, and will receive more improvements in the future.For the most part, Generator can be used as a replacement for RandomState.See Legacy random generation for information on the legacy infrastructure,What’s new or different for information on transitioning, and NEP 19 for some of the reasoning for the transition.

Design#

Users primarily interact with Generator instances. Each Generator instanceowns a BitGenerator instance that implements the core RNG algorithm. TheBitGenerator has a limited set of responsibilities. It manages state andprovides functions to produce random doubles and random unsigned 32- and 64-bitvalues.

The Generator takes the bit generator-provided stream and transforms theminto more useful distributions, e.g., simulated normal random values. Thisstructure allows alternative bit generators to be used with little codeduplication.

NumPy implements several different BitGenerator classes implementingdifferent RNG algorithms. default_rng currently uses PCG64 as thedefault BitGenerator. It has better statistical properties and performancethan the MT19937 algorithm used in the legacy RandomState. SeeBit generators for more details on the supported BitGenerators.

default_rng and BitGenerators delegate the conversion of seeds into RNGstates to SeedSequence internally. SeedSequence implements a sophisticatedalgorithm that intermediates between the user’s input and the internalimplementation details of each BitGenerator algorithm, each of which canrequire different amounts of bits for its state. Importantly, it lets you usearbitrary-sized integers and arbitrary sequences of such integers to mixtogether into the RNG state. This is a useful primitive for constructinga flexible pattern for parallel RNG streams.

For backward compatibility, we still maintain the legacy RandomState class.It continues to use the MT19937 algorithm by default, and old seeds continueto reproduce the same results. The convenience Functions in numpy.randomare still aliases to the methods on a single global RandomState instance. SeeLegacy random generation for the complete details. See What’s new or different fora detailed comparison between Generator and RandomState.

Parallel Generation#

The included generators can be used in parallel, distributed applications ina number of ways:

  • SeedSequence spawning

  • Sequence of integer seeds

  • Independent streams

  • Jumping the BitGenerator state

Users with a very large amount of parallelism will want to consultUpgrading PCG64 with PCG64DXSM.

Concepts#

  • Random Generator
  • Legacy Generator (RandomState)
  • Bit generators
  • Seeding and entropy
  • Upgrading PCG64 with PCG64DXSM
  • Compatibility policy

Features#

  • Parallel Applications
    • SeedSequence spawning
    • Sequence of integer seeds
    • Independent streams
    • Jumping the BitGenerator state
  • Multithreaded Generation
  • What’s new or different
  • Comparing Performance
    • Recommendation
    • Timings
    • Performance on different operating systems
  • C API for random
  • Examples of using Numba, Cython, CFFI
    • Numba
    • Cython
    • CFFI
    • New BitGenerators
    • Examples

Original Source of the Generator and BitGenerators#

This package was developed independently of NumPy and was integrated in version1.17.0. The original repo is at bashtage/randomgen.

Random sampling (numpy.random) — NumPy v2.0 Manual (2024)

References

Top Articles
Latest Posts
Article information

Author: Greg O'Connell

Last Updated:

Views: 6614

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Greg O'Connell

Birthday: 1992-01-10

Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

Phone: +2614651609714

Job: Education Developer

Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.