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.
randomstate.entropy.random_entropy()
provides access to the system
source of randomness that is used in cryptographic applications (e.g.,
/dev/urandom
on Unix).complex_normal()
)standard_normal()
,
standard_exponential()
or
standard_gamma()
.
The Ziggurat generator can be accessed by passing the keyword
argument method='zig'
.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)
dtype
argument that accepts np.float32
or np.float64
to produce either single or double prevision uniform random variables for
select distributionsrandom_sample()
and
rand()
)standard_normal()
and
randn()
)standard_gamma()
)standard_exponential()
)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)
Optional out
argument that allows existing arrays to be filled for
select distributions
random_sample()
)standard_normal()
)standard_gamma()
)standard_exponential()
)This allows multithreading to fill large arrays in chunks using suitable PRNGs in parallel.
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)