How to generate reproducible random numbers and the scope of the seed
What is the problem?
introduction
We may know that the computer is using a random number generator to generate random numbers. Since it is a pseudo-random number generator, actually, we can generate repeated random numbers if we fix the random number generator. Specifically, we can set up a fixed seed.
Fixed random numbers are helpful when we want to have a fair comparison of different algorithms and want different algorithms to use the same random inputs.
Problem
We can do it by setting the seed of a random number generator. However, I am not quite clear about the scope of the random number seed. This is the problem I am trying to make it clear.
How to make clear about this problem
In order to be clear, I am writing a code to test the scope of the random number generator seed.
code
# I am not sure about the random number seed's scope
# I'd like to test it hereimport numpy as npa = np.arange(100)
np.random.seed(42)
np.random.shuffle(a)
print(a)random1 = []
for i in range(10):
random1.append(np.random.randint(0, 20))random2 = []
for i in range(10):
random2.append(np.random.randint(0, 20))
print("random1", random1)
print("random2", random2)
The output of the above code by running the same code twice
(base) python test_random_seed.py
[83 53 70 45 44 39 22 80 10 0 18 30 73 33 90 4 76 77 12 31 55 88 26 42
69 15 40 96 9 72 11 47 85 28 93 5 66 65 35 16 49 34 7 95 27 19 81 25
62 13 24 3 17 38 8 78 6 64 36 89 56 99 54 43 50 67 46 68 61 97 79 41
58 48 98 57 75 32 94 59 63 84 37 29 1 52 21 2 23 87 91 74 86 82 20 60
71 14 92 51]
random1 [18, 16, 7, 2, 2, 0, 4, 9, 6, 8]
random2 [6, 8, 7, 11, 1, 0, 15, 4, 2, 11](base)python test_random_seed.py
[83 53 70 45 44 39 22 80 10 0 18 30 73 33 90 4 76 77 12 31 55 88 26 42
69 15 40 96 9 72 11 47 85 28 93 5 66 65 35 16 49 34 7 95 27 19 81 25
62 13 24 3 17 38 8 78 6 64 36 89 56 99 54 43 50 67 46 68 61 97 79 41
58 48 98 57 75 32 94 59 63 84 37 29 1 52 21 2 23 87 91 74 86 82 20 60
71 14 92 51]
random1 [18, 16, 7, 2, 2, 0, 4, 9, 6, 8]
random2 [6, 8, 7, 11, 1, 0, 15, 4, 2, 11]
Conclusion:
From the results, it seems that the scope of the random number seed covers the whole code.
A little bit deeper
From this post, the poster mentioned that
“The CPython random.py
implementation is very readable. I recommend having a look: https://github.com/python/cpython/blob/3.6/Lib/random.py”
I think it should be a way to have a deeper understanding of the random package in python. Meanwhile, in the example code, I am using NumPy, I think read the source code of NumPy will also be helpful.
As I am run out of time on my project, I will not explore the source code. If any reader wants to try and find something interesting, please leave me a comment. Thanks a lot.
Thanks for reading.