np.random.seed(0) makes the random numbers predictable
>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55, 0.72, 0.6 , 0.54])
>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55, 0.72, 0.6 , 0.54])
With the seed reset (every time), the same set of numbers will appear every time.
If the random seed is not reset, different numbers appear with every invocation:
>>> numpy.random.rand(4)
array([ 0.42, 0.65, 0.44, 0.89])
>>> numpy.random.rand(4)
array([ 0.96, 0.38, 0.79, 0.53])
(pseudo-)random numbers work by starting with a number (the seed), multiplying it by a large number, adding an offset, then taking modulo of that sum. The resulting number is then used as the seed to generate the next "random" number. When you set the seed (every time), it does the same thing every time, giving you the same numbers.
If you want seemingly random numbers, do not set the seed. If you have code that uses random numbers that you want to debug, however, it can be very helpful to set the seed before each run so that the code does the same thing every time you run it.
To get the most random numbers for each run, call numpy.random.seed(). This will cause numpy to set the seed to a random number obtained from /dev/urandom or its Windows analog or, if neither of those is available, it will use the clock.
For more information on using seeds to generate pseudo-random numbers, see wikipedia.
Answer from John1024 on Stack Overflownp.random.seed(0) makes the random numbers predictable
>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55, 0.72, 0.6 , 0.54])
>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55, 0.72, 0.6 , 0.54])
With the seed reset (every time), the same set of numbers will appear every time.
If the random seed is not reset, different numbers appear with every invocation:
>>> numpy.random.rand(4)
array([ 0.42, 0.65, 0.44, 0.89])
>>> numpy.random.rand(4)
array([ 0.96, 0.38, 0.79, 0.53])
(pseudo-)random numbers work by starting with a number (the seed), multiplying it by a large number, adding an offset, then taking modulo of that sum. The resulting number is then used as the seed to generate the next "random" number. When you set the seed (every time), it does the same thing every time, giving you the same numbers.
If you want seemingly random numbers, do not set the seed. If you have code that uses random numbers that you want to debug, however, it can be very helpful to set the seed before each run so that the code does the same thing every time you run it.
To get the most random numbers for each run, call numpy.random.seed(). This will cause numpy to set the seed to a random number obtained from /dev/urandom or its Windows analog or, if neither of those is available, it will use the clock.
For more information on using seeds to generate pseudo-random numbers, see wikipedia.
If you set the np.random.seed(a_fixed_number) every time you call the numpy's other random function, the result will be the same:
>>> import numpy as np
>>> np.random.seed(0)
>>> perm = np.random.permutation(10)
>>> print perm
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0)
>>> print np.random.permutation(10)
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0)
>>> print np.random.permutation(10)
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0)
>>> print np.random.permutation(10)
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0)
>>> print np.random.rand(4)
[0.5488135 0.71518937 0.60276338 0.54488318]
>>> np.random.seed(0)
>>> print np.random.rand(4)
[0.5488135 0.71518937 0.60276338 0.54488318]
However, if you just call it once and use various random functions, the results will still be different:
>>> import numpy as np
>>> np.random.seed(0)
>>> perm = np.random.permutation(10)
>>> print perm
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0)
>>> print np.random.permutation(10)
[2 8 4 9 1 6 7 3 0 5]
>>> print np.random.permutation(10)
[3 5 1 2 9 8 0 6 7 4]
>>> print np.random.permutation(10)
[2 3 8 4 5 1 0 6 9 7]
>>> print np.random.rand(4)
[0.64817187 0.36824154 0.95715516 0.14035078]
>>> print np.random.rand(4)
[0.87008726 0.47360805 0.80091075 0.52047748]
Is there a way to set the seed for numpy.random for an entire script (aka not have to set it every time you call the RNG)?
python - How to set the fixed random seed in numpy? - Stack Overflow
Is there a way to set the seed for numpy.random for an entire script (aka not have to set it every time you call the RNG)?
Explain random.seed() like I’m five.
Videos
OK, I got the exactly same problem as it was posted 4 years ago in the sub. https://www.reddit.com/r/learnpython/comments/du4f22/is_there_a_way_to_set_the_seed_for_numpyrandom/, but it seems to be not answered directly (the usual "you shouldn't do this" stuff).
Specifically about my problem, I am writing this GUI that down-samples the data for visualization. The "down sampling" is just to avoid slowing down the matplotlib plotting engine. I want the user to see the same plot every time they look at the data with the same settings / input.
Code here:
import numpy as np
import matplotlib.pyplot as plt
import time
# Generate fake data
x = np.random.normal(size=2000)
y = x * 2 + np.random.normal(size=2000)
sampleRNG = np.random.default_rng(42)
# The for loop to simulate user inputing differnt down-sample settings
for down_sample_size in [500, 600, 200, 500, 700]:
# problem solved if the following line is not commented out
# sampleRNG = np.random.default_rng(42)
randIndex = sampleRNG.choice(len(x), size=down_sample_size, replace=False, axis=0, shuffle=False)
fig, ax = plt.subplots()
ax.scatter(x[randIndex], y[randIndex], s=2)
plt.show()
time.sleep(1)
Note, the "for loop" in the above code is just for mimicking users changing the down-sample settings to look at the data again. My goal is to have the first and forth plot (both with a down-sample size at 500) looks the same. This will be resolved if I put the sampleRNG = np.random.default_rng(42) inside the loop, but I am wondering if this is the right thing to do.
Method I have considered to get around:
-
Shuffle the sample when it's loaded, and only take the first N points of the sample when user requires to down sample to N. This is like pre-rendering the data, but it will take double the memory, as I do want to keep the original data order.
Feedback is welcome....
np.random.seed is a function, which you need to call, not assign to it. E.g.:
np.random.seed(42)
np.random.seed is function that sets the random state globally. As an alternative, you can also use np.random.RandomState(x) to instantiate a random state class to obtain reproducibility locally. Adapted from your code, I provide an alternative option as follows.
import numpy as np
random_state = 100
rng=np.random.RandomState(random_state )
mu, sigma = 0, 0.25
eps = rng.normal(mu,sigma,size=100) # Difference here
print(eps[0])
More details on np.random.seed and np.random.RandomState can be found here.