randomstate’s documentation

Warning

End-of-life notification

This library was designed to bring alternative generators to the NumPy infrastructure. It as been successful in advancing the conversation for a future implementation of a new random number API in NumPy which will allow new algorithms and/or generators. The next step in this process is to separate the basic (or core RNG) from the functions that transform random bits into useful random numbers. This has been implemented in a successor project randomgen available on GitHub or PyPi.

randomgen has a slightly different API, so please see the randomgen documentation.

This package contains drop-in replacements for the NumPy RandomState object that change the core random number generator.

What’s New or Different

In [1]: from randomstate.prng.xoroshiro128plus import standard_normal

In [2]: %timeit standard_normal(1000000, method='bm')
   ...: %timeit standard_normal(1000000, method='zig')
   ...: 
43.4 ms +- 301 us per loop (mean +- std. dev. of 7 runs, 10 loops each)
9.89 ms +- 60.5 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
In [3]: from randomstate.prng.xoroshiro128plus import standard_exponential

In [4]: %timeit standard_exponential(1000000, method='inv')
   ...: %timeit standard_exponential(1000000, method='zig')
   ...: 
57.1 ms +- 458 us per loop (mean +- std. dev. of 7 runs, 10 loops each)
5.32 ms +- 68 us per loop (mean +- std. dev. of 7 runs, 100 loops each)
In [5]: from randomstate.prng.xoroshiro128plus import standard_gamma

In [6]: %timeit standard_gamma(3.0, 1000000, method='inv')
   ...: %timeit standard_gamma(3.0, 1000000, method='zig')
   ...: 
57.7 ms +- 263 us per loop (mean +- std. dev. of 7 runs, 10 loops each)
27.5 ms +- 346 us per loop (mean +- std. dev. of 7 runs, 10 loops each)
In [7]: import randomstate as rs

In [8]: rs.seed(0)

In [9]: rs.random_sample(3, dtype='d')
Out[9]: array([0.5488135 , 0.71518937, 0.60276338])

In [10]: rs.seed(0)

In [11]: rs.random_sample(3, dtype='f')
Out[11]: array([0.54881346, 0.5928446 , 0.71518934], dtype=float32)
In [12]: import numpy as np

In [13]: import randomstate as rs

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

In [15]: rs.seed(0)

In [16]: rs.random_sample(out=existing[:2])
Out[16]: array([0.5488135 , 0.71518937])

In [17]: print(existing)
[0.5488135  0.71518937 0.         0.        ]
  • For changes since the previous release, see the Change Log

Parallel Generation

The included generators can be used in parallel, distributed applications in one of two ways:

Supported Generators

The main innovation is the inclusion of a number of alternative pseudo-random number generators, ‘in addition’ to the standard PRNG in NumPy. The included PRNGs are:

  • MT19937 - The standard NumPy generator. Produces identical results to NumPy using the same seed/state. Adds a jump function that advances the generator as-if 2**128 draws have been made (randomstate.prng.mt19937.jump()). See NumPy’s documentation.
  • SFMT and dSFMT - SSE2 enabled versions of the MT19937 generator. Theoretically the same, but with a different state and so it is not possible to produce a sequence identical to MT19937. Both generators support jump and so can be used in parallel applications. See the dSFMT authors’ page.
  • XoroShiro128+ - Improved version of XorShift128+ with better performance and statistical quality. Like the XorShift generators, it can be jumped to produce multiple streams in parallel applications. See randomstate.prng.xoroshiro128plus.jump() for details. More information about this PRNG is available at the xorshift and xoroshiro authors’ page.
  • XorShit128+ and XorShift1024* - Vast fast generators based on the XSadd generator. These generators support jump and so can be used in parallel applications. See the documentation for randomstate.prng.xorshift1024.jump() for details. More information about these PRNGs is available at the xorshift and xoroshiro authors’ page.
  • PCG-32 and PCG-64 - Fast generators that support many parallel streams and can be advanced by an arbitrary amount. See the documentation for randomstate.prng.pcg64.advance(). PCG-32 only as a period of \(2^{64}\) while PCG-64 has a period of \(2^{128}\). See the PCG author’s page for more details about this class of PRNG.
  • Multiplicative Lagged Fibonacci Generator MLFG(1279, 861, *) - A directly implemented multiplicative lagged Fibonacci generator with a very large period and good performance. Future plans include multiple stream support. See the wiki page on Fibonacci generators.
  • MRG32K3A - a classic and popular generator by L’Ecuyer. Future plans include multiple stream support. See the MRG32K3A author’s page. Lower performance than more modern generators.

Indices and tables