Difference between np.random.seed() and np.random.RandomState()

PythonNumpyRandom

Python Problem Overview


I know that to seed the randomness of numpy.random, and be able to reproduce it, I should us:

import numpy as np
np.random.seed(1234)

but what does np.random.RandomState() do?

Python Solutions


Solution 1 - Python

If you want to set the seed that calls to np.random... will use, use np.random.seed:

np.random.seed(1234)
np.random.uniform(0, 10, 5)
#array([ 1.9151945 ,  6.22108771,  4.37727739,  7.85358584,  7.79975808])
np.random.rand(2,3)
#array([[ 0.27259261,  0.27646426,  0.80187218],
#       [ 0.95813935,  0.87593263,  0.35781727]])

Use the class to avoid impacting the global numpy state:

r = np.random.RandomState(1234)
r.uniform(0, 10, 5)
#array([ 1.9151945 ,  6.22108771,  4.37727739,  7.85358584,  7.79975808])

And it maintains the state just as before:

r.rand(2,3)
#array([[ 0.27259261,  0.27646426,  0.80187218],
#       [ 0.95813935,  0.87593263,  0.35781727]])

You can see the state of the sort of 'global' class with:

np.random.get_state()

and of your own class instance with:

r.get_state()

Solution 2 - Python

np.random.RandomState() constructs a random number generator. It does not have any effect on the freestanding functions in np.random, but must be used explicitly:

>>> rng = np.random.RandomState(42)
>>> rng.randn(4)
array([ 0.49671415, -0.1382643 ,  0.64768854,  1.52302986])
>>> rng2 = np.random.RandomState(42)
>>> rng2.randn(4)
array([ 0.49671415, -0.1382643 ,  0.64768854,  1.52302986])

Solution 3 - Python

random.seed is a method to fill random.RandomState container.

from numpy docs:

numpy.random.seed(seed=None)

>Seed the generator. >
>This method is called when RandomState is initialized. It can be called again to re-seed the generator. For details, see RandomState.

class numpy.random.RandomState

>Container for the Mersenne Twister pseudo-random number generator.

Solution 4 - Python

np.random.RandomState() - a class that provides several methods based on different probability distributions.
np.random.RandomState.seed() - called when RandomState() is initialised.

Solution 5 - Python

Seed is a global pseudo-random generator. However, randomstate is a pseudo-random generator isolated from others, which only impact specific variable.

rng = np.random.RandomState(0)
rng.rand(4)
# Out[1]: array([0.5488135 , 0.71518937, 0.60276338, 0.54488318])
rng = np.random.RandomState(0)
rng.rand(4)
# Out[2]: array([0.5488135 , 0.71518937, 0.60276338, 0.54488318])

It's basically as same as Seed, but as the following, We don't assign randomstate to a variable.

np.random.RandomState(0)
# Out[3]: <mtrand.RandomState at 0xddaa288>
np.random.rand(4)
# Out[4]: array([0.62395295, 0.1156184 , 0.31728548, 0.41482621])
np.random.RandomState(0)
# Out[5]: <mtrand.RandomState at 0xddaac38>
np.random.rand(4)
# Out[6]: array([0.86630916, 0.25045537, 0.48303426, 0.98555979])

The latter is different from the former. It means that randomstate only avails inside specific variable.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioneranView Question on Stackoverflow
Solution 1 - PythonaskewchanView Answer on Stackoverflow
Solution 2 - PythonFred FooView Answer on Stackoverflow
Solution 3 - PythonBruno GelbView Answer on Stackoverflow
Solution 4 - PythonShruti SharmaView Answer on Stackoverflow
Solution 5 - PythonzfireearView Answer on Stackoverflow