Random Number Generation¶
Numpy’s random number generation¶
Stochastic relies on numpy for random number generation. Since numpy 1.17, the newer Generator
objects provide improved performance:
Note
From numpy docs: The Generator’s normal, exponential and gamma functions use 256-step Ziggurat methods which are 2-10 times faster than NumPy’s Box-Muller or inverse CDF implementations.
By default, the stochastic package uses numpy’s faster Generator random number generation. With a function call, we can change the default back to the legacy random number generation, which uses RandomState objects.
If no rng
arg is passed when instantiating process instances, each instance will reference the stochastic.random
module’s generator
attribute for random number generation.
Examples¶
Changing the default random number generation on instances without specified rng
:
from stochastic.processes import GaussianNoise
from stochastic import random
gn = GaussianNoise()
print(gn.rng)
# Generator(PCG64)
# use the legacy random number generator
random.use_randomstate()
print(gn.rng)
# <module 'numpy.random' from '/path/to/site-packages/numpy/random/__init__.py'>
# use the newer Generator
random.use_generator()
print(gn.rng)
# Generator(PCG64)
Setting the seed value:
from stochastic.processes import GaussianNoise
from stochastic import random
gn = GaussianNoise()
print(gn.rng)
# Generator(PCG64)
random.seed(42)
print(gn.rng.bit_generator.state)
# {'bit_generator': 'PCG64', 'state': {'state': 274674114334540486603088602300644985544, 'inc': 332724090758049132448979897138935081983}, 'has_uint32': 0, 'uinteger': 0}
print(gn.sample(4))
# [ 0.15235854 -0.51999205 0.3752256 0.47028236]
random.seed(42)
print(gn.rng.bit_generator.state)
# {'bit_generator': 'PCG64', 'state': {'state': 274674114334540486603088602300644985544, 'inc': 332724090758049132448979897138935081983}, 'has_uint32': 0, 'uinteger': 0}
print(gn.sample(4))
# [ 0.15235854 -0.51999205 0.3752256 0.47028236]
Passing custom generators to process instances at instantiation:
from numpy.random import Generator
from numpy.random import PCG64
from stochastic.processes import GaussianNoise
from stochastic import random
generator = Generator(PCG64(seed=42))
gn = GaussianNoise(rng=generator)
# generator specific to this gaussian noise instance
print(gn.rng.bit_generator.state)
# {'bit_generator': 'PCG64', 'state': {'state': 274674114334540486603088602300644985544, 'inc': 332724090758049132448979897138935081983}, 'has_uint32': 0, 'uinteger': 0}
# stochastic's global generator, different from the one attached to `gn`
print(random.generator.bit_generator.state)
# {'bit_generator': 'PCG64', 'state': {'state': 228239801863081385502825691348763076514, 'inc': 61631449755775032062670113901777656135}, 'has_uint32': 0, 'uinteger': 0}
Documentation¶
-
stochastic.random.
generator
= Generator(PCG64) at 0x7FE9A17D09E0¶ The default random number generator for the stochastic package
-
stochastic.random.
seed
(value)[source]¶ Sets the seed for numpy legacy or
default_rng
generators.If using the legacy generator, this will call
numpy.random.seed(value)
. Otherwise a new random number generator is created usingnumpy.random.default_rng(value)
.
-
stochastic.random.
use_generator
(rng=None)[source]¶ Use the new numpy Generator as default for stochastic.
Sets the default random number generator for stochastic processes to the newer
np.random.default_rng()
.Note
This is the default generator and there is no need to call this function unless returning to the default after switching away from it.
- Parameters
rng (numpy.random.Generator) – a Generator instance to use as the default random number generator for stochastic.
-
stochastic.random.
use_randomstate
(rng=None)[source]¶ Use the legacy numpy RandomState generator as default for stochastic.
Sets the default random number generator for stochastic processes to the legacy
np.random
.- Parameters
rng (numpy.random.RandomState) – a RandomState instance to use as the default random number generator for stochastic.