I have a big simulation where I set a seed value (through the random.seed) for the random module in the main file. The main file imports the random module and other files, which import random module too. When I run the simulation, I hope the setted seed is shared to imported files, but as imported files are executed first than main file, the random module in that files reset the seed every time. Below, a reproducible example.
File main.py:
# main.py
import foo
import random
random.seed (10)
main = random.randint(1, 100)
print("main: ", main)
File foo.py:
# foo.py
import random
foo = random.randint(1, 100)
print("foo: ", foo)
When I execute python main.py
multiple times the value of the main
variable is always the same, as I expected. But the value of foo
variable changes every time. How can I can set the seed in the main file such that same seed will be used in imported files?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…