What’s new or different#

NumPy 1.17.0 introduced Generator as an improved replacement for the legacy RandomState. Here is a quick comparison of the two implementations.

Feature

Older Equivalent

Notes

Generator

RandomState

Generator requires a stream source, called a BitGenerator A number of these are provided. RandomState uses the Mersenne Twister MT19937 by default, but can also be instantiated with any BitGenerator.

random

random_sample, rand

Access the values in a BitGenerator, convert them to float64 in the interval [0.0., 1.0). In addition to the size kwarg, now supports dtype='d' or dtype='f', and an out kwarg to fill a user-supplied array.

Many other distributions are also supported.

integers

randint, random_integers

Use the endpoint kwarg to adjust the inclusion or exclusion of the high interval endpoint.

  • The normal, exponential and gamma generators use 256-step Ziggurat methods which are 2-10 times faster than NumPy’s default implementation in standard_normal, standard_exponential or standard_gamma. Because of the change in algorithms, it is not possible to reproduce the exact random values using Generator for these distributions or any distribution method that relies on them.

In [1]: import numpy.random

In [2]: rng = np.random.default_rng()

In [3]: %timeit -n 1 rng.standard_normal(100000)
   ...: %timeit -n 1 numpy.random.standard_normal(100000)
   ...: 
1.17 ms +- 16.5 us per loop (mean +- std. dev. of 7 runs, 1 loop each)
2.26 ms +- 28.1 us per loop (mean +- std. dev. of 7 runs, 1 loop each)
In [4]: %timeit -n 1 rng.standard_exponential(100000)
   ...: %timeit -n 1 numpy.random.standard_exponential(100000)
   ...: 
620 us +- 18.5 us per loop (mean +- std. dev. of 7 runs, 1 loop each)
1.58 ms +- 8.17 us per loop (mean +- std. dev. of 7 runs, 1 loop each)
In [5]: %timeit -n 1 rng.standard_gamma(3.0, 100000)
   ...: %timeit -n 1 numpy.random.standard_gamma(3.0, 100000)
   ...: 
2.51 ms +- 440 us per loop (mean +- std. dev. of 7 runs, 1 loop each)
4.36 ms +- 35.8 us per loop (mean +- std. dev. of 7 runs, 1 loop each)
In [6]: rng = np.random.default_rng()

In [7]: rng.random(3, dtype=np.float64)
Out[7]: array([0.56503194, 0.69009308, 0.27825561])

In [8]: rng.random(3, dtype=np.float32)
Out[8]: array([0.8049718 , 0.36848146, 0.7987144 ], dtype=float32)

In [9]: rng.integers(0, 256, size=3, dtype=np.uint8)
Out[9]: array([182, 109, 144], dtype=uint8)
  • Optional out argument that allows existing arrays to be filled for select distributions

    This allows multithreading to fill large arrays in chunks using suitable BitGenerators in parallel.

In [10]: rng = np.random.default_rng()

In [11]: existing = np.zeros(4)

In [12]: rng.random(out=existing[:2])
Out[12]: array([0.80684028, 0.02131013])

In [13]: print(existing)
[0.80684028 0.02131013 0.         0.        ]
  • Optional axis argument for methods like choice, permutation and shuffle that controls which axis an operation is performed over for multi-dimensional arrays.

In [14]: rng = np.random.default_rng()

In [15]: a = np.arange(12).reshape((3, 4))

In [16]: a
Out[16]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [17]: rng.choice(a, axis=1, size=5)
Out[17]: 
array([[ 2,  2,  1,  0,  3],
       [ 6,  6,  5,  4,  7],
       [10, 10,  9,  8, 11]])

In [18]: rng.shuffle(a, axis=1)        # Shuffle in-place

In [19]: a
Out[19]: 
array([[ 0,  3,  2,  1],
       [ 4,  7,  6,  5],
       [ 8, 11, 10,  9]])
  • Added a method to sample from the complex normal distribution (complex_normal)