My question is related to What is the scope of a random seed in Python? .
In the case of above question, it is clarified that there is a (hidden) global Random()
instance in the module for random
.
1) I would like to clarify whether setting the random seed in one module will cause this to be the random seed in other modules and whether there are certain things to be aware of.
For instance: Given: moduleA.py, moduleB.py
moduleA.py:
import random
import moduleB
random.seed(my_seed)
moduleB.randomfct()
moduleB.py:
import random
def randomfct():
#do_things_using_random
Does moduleB also use my_seed
, or do I have to pass the seed to moduleB.py and set it again?
2) Does the order of setting the random seed / importing play any role?
For example in moduleA.py
:
import random
random.seed(my_seed)
import moduleB
3) Is this also the case for setting numpy random seeds, e.g. np.random.seed(42)
?
Python Random seed() Method The seed() method is used to initialize the random number generator. The random number generator needs a number to start with (a seed value), to be able to generate a random number. By default the random number generator uses the current system time.
The numpy random seed is a numerical value that generates a new set or repeats pseudo-random numbers. The value in the numpy random seed saves the state of randomness. If we call the seed function using value 1 multiple times, the computer displays the same random numbers.
seed(number) sets what NumPy calls the global random seed, which affects all uses to the np. random. * module. Some imported packages or other scripts could reset the global random seed to another random seed with np.
Generating random numbers with numpy In both ways, we are using what we call a pseudo random number generator or PRNG. Indeed, whenever we call a python function, such as np. random. rand() the output can only be deterministic and cannot be truly random.
The CPython random.py
implementation is very readable. I recommend having a look: https://github.com/python/cpython/blob/3.6/Lib/random.py
Anyway, that version of python creates a global random.Random()
object and assigns it directly to the random
module. This object contains a seed(a)
method which acts as a module function when you call random.seed(a)
. Thus the seed state is shared across your entire program.
1) Yes. moduleA
and moduleB
uses the same seed. Importing random
in moduleA
creates the global random.Random()
object. Reimporting it in moduleB
just gives you the same module and maintains the originally created random.Random()
object.
2) No. Not in the example you gave, but in general yes it can matter. You might use moduleB
before you set the seed in moduleA
thus your seed wasn't set.
3) Hard to tell. Much more complicated code base. That said, I would think it works the same way. The authors of numpy would really have to try to make it work in a different way than how it works in the python implementation.
In general, if you are worried about seed state, I recommend creating your own random objects and pass them around for generating random numbers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With