Skip to main content
`Ctrl`+`K`

# Random sampling

## 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 of probability distributions. In general, users will create a `Generator` instance with `default_rng` and call the various methods on it to obtain samples from different distributions.

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

Warning

The pseudo-random number generators implemented in this module are designed for statistical modeling and simulation. They are not suitable for security or cryptographic purposes. See the `secrets` module from the standard library for such use cases.

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

See the documentation on `default_rng` and `SeedSequence` for more advanced options for controlling the seed in specialized scenarios.

`Generator` and its associated infrastructure was introduced in NumPy version 1.17.0. There is still a lot of code that uses the older `RandomState` and the functions in `numpy.random`. While there are no plans to remove them at this time, we do recommend transitioning to `Generator` as you can. The algorithms are 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` instance owns a `BitGenerator` instance that implements the core RNG algorithm. The `BitGenerator` has a limited set of responsibilities. It manages state and provides functions to produce random doubles and random unsigned 32- and 64-bit values.

The `Generator` takes the bit generator-provided stream and transforms them into more useful distributions, e.g., simulated normal random values. This structure allows alternative bit generators to be used with little code duplication.

NumPy implements several different `BitGenerator` classes implementing different RNG algorithms. `default_rng` currently uses `PCG64` as the default `BitGenerator`. It has better statistical properties and performance than the `MT19937` algorithm used in the legacy `RandomState`. See Bit generators for more details on the supported BitGenerators.

`default_rng` and BitGenerators delegate the conversion of seeds into RNG states to `SeedSequence` internally. `SeedSequence` implements a sophisticated algorithm that intermediates between the user’s input and the internal implementation details of each `BitGenerator` algorithm, each of which can require different amounts of bits for its state. Importantly, it lets you use arbitrary-sized integers and arbitrary sequences of such integers to mix together into the RNG state. This is a useful primitive for constructing a 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 continue to reproduce the same results. The convenience Functions in numpy.random are still aliases to the methods on a single global `RandomState` instance. See Legacy random generation for the complete details. See What’s new or different for a detailed comparison between `Generator` and `RandomState`.

### Parallel Generation

The included generators can be used in parallel, distributed applications in a 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 consult Upgrading 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
  - `bitgen_t`
  - `random_standard_uniform`
  - `random_standard_uniform_fill`
  - `random_standard_exponential`
  - `random_standard_exponential_fill`
  - `random_standard_exponential_inv_fill`
  - `random_standard_normal`
  - `random_standard_normal_fill`
  - `random_standard_normal_fill_f`
  - `random_standard_gamma`
  - `random_standard_uniform_f`
  - `random_standard_uniform_fill_f`
  - `random_standard_exponential_f`
  - `random_standard_exponential_fill_f`
  - `random_standard_exponential_inv_fill_f`
  - `random_standard_normal_f`
  - `random_standard_gamma_f`
  - `random_normal`
  - `random_gamma`
  - `random_gamma_f`
  - `random_exponential`
  - `random_uniform`
  - `random_beta`
  - `random_chisquare`
  - `random_f`
  - `random_standard_cauchy`
  - `random_pareto`
  - `random_weibull`
  - `random_power`
  - `random_laplace`
  - `random_gumbel`
  - `random_logistic`
  - `random_lognormal`
  - `random_rayleigh`
  - `random_standard_t`
  - `random_noncentral_chisquare`
  - `random_noncentral_f`
  - `random_wald`
  - `random_vonmises`
  - `random_triangular`
  - `random_poisson`
  - `random_negative_binomial`
  - `binomial_t`
  - `random_binomial`
  - `random_logseries`
  - `random_geometric_search`
  - `random_geometric_inversion`
  - `random_geometric`
  - `random_zipf`
  - `random_hypergeometric`
  - `random_interval`
  - `random_multinomial`
  - `random_multivariate_hypergeometric_count`
  - `random_multivariate_hypergeometric_marginals`
  - `random_positive_int64`
  - `random_positive_int32`
  - `random_positive_int`
  - `random_uint`
  - `random_bounded_uint64`
- 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 version 1.17.0. The original repo is at bashtage/randomgen.