Is there a good way to split random seed between modules (in python)?

I have a project with different main files (for different simulations). When I run one of the main files, it should set a random seed (and numpy.random), and all modules in the project should use this seed.

I have not found a good way to do this. I have a globals.py file with this:

import random myRandom=None def initSeed(seed): global myRandom myRandom =random.Random(seed) 

then from the main I do:

 if __name__ == "__main__": seed=10 globals.initSeed(seed) ... 

Then in the modules that cause the main calls, I do:

 from globals import myRandom 

But myRandom has the value None in the module (although I modified it in main!). Why and how to fix it? Is there a better way?

+6
source share
2 answers

I would use a file to avoid global and separate data and logic a bit.

seed_handler.py

 # file that stores the shared seed value seed_val_file = "seed_val.txt" def save_seed(val, filename=seed_val_file): """ saves val. Called once in simulation1.py """ with open(filename, "wb") as f: f.write(str(val)) def load_seed(filename=seed_val_file): """ loads val. Called by all scripts that need the shared seed value """ with open(filename, "rb") as f: # change datatype accordingly (numpy.random.random() returns a float) return int(f.read()) 

simulation1.py

 import random import seed_handler def sim1(): """ creates a new seed and prints a deterministic "random" number """ new_seed = int("DEADBEEF",16) # Replace with numpy.random.random() or whatever print "New seed:", new_seed # do the actual seeding of the pseudo-random number generator random.seed(new_seed) # the result print "Random: ", random.random() # save the seed value so other scripts can use it seed_handler.save_seed(new_seed) if __name__ == "__main__": sim1() 

simulation2.py

 import random import seed_handler def sim2(): """ loads the old seed and prints a deterministic "random" number """ old_seed = seed_handler.load_seed() print "Old seed:", old_seed # do the actual seeding of the pseudo-random number generator random.seed(old_seed) # the result print "Random: ", random.random() if __name__ == "__main__": sim2() 

Output:

 user@box :~/$ python simulation1.py New seed: 3735928559 Random: 0.0191336454935 user@box :~/$ python simulation2.py Old seed: 3735928559 Random: 0.0191336454935 

ADDITION

I just read in the comments that this is for research. Currently, the execution of the simulation1.py function overwrites the stored initial value; this may be undesirable. You can add one of the following functions:

  • save as json and load into the dictionary; this way, nothing will be overwritten, and each initial value can have notes, a time stamp, and a user’s label associated with it.
  • just ask the user about yes / no to overwrite the existing value.
+4
source
  • As mentioned in the @jDo comment, renamed randGlobal.py to randGlobal.py .

  • Added testResult.py module for testing.


randGlobal.py

 import random def initSeed(seed): # declare global myRandom myRandom = random.Random(seed) 

testResult.py

 import randGlobal def randOutput(): return randGlobal.myRandom.random() 

main.py

 import randGlobal # Add a module for testing import testResult def test(): result = testResult.randOutput() print result # main if __name__ == "__main__": seed=10 randGlobal.initSeed(seed) # after init, hava a test test() 
+1
source

All Articles