I'm trying to reproduce a simulation that I ran previously, such that I record down the seed of current date time inside a text file, and then use the recorded date time seed to get the same values that I got previously
However, I'm not sure why the values that come out isn't similar to the ones I ran in the previous simulation.
This is what I got when I tried to run the program:
=================== RESTART: /Users/ivanteong/Desktop/e.py ===================
Choose 1 to run simulation based on random seed of current time, or choose 2 to reproduce past simulation: 1
2017-05-20 18:55:51
0.902032491409618
0.33535058732344564
>>>
=================== RESTART: /Users/ivanteong/Desktop/e.py ===================
Choose 1 to run simulation based on random seed of current time, or choose 2 to reproduce past simulation: 2
Enter the seed of current time recorded: 2017-05-20-18-55-51
2017-05-20 18:55:51
0.759062526352241
0.058976331409061576
>>>
The code is below.
import math
import random
from datetime import datetime
# reproducibility
reproduce = int(input("Choose 1 to run simulation based on random seed of current time, or choose 2 to reproduce past simulation: "))
if reproduce == 1:
# seeding random based on current time and writing into text file for reproducibility
string_seed = datetime.strftime(datetime.now(), '%Y-%m-%d-%H-%M-%S')
f = open('seed.txt', 'a')
f.write(str(string_seed))
f.write('\n')
f.close()
seed = datetime.strptime(string_seed, '%Y-%m-%d-%H-%M-%S')
print(seed)
elif reproduce == 2:
stored_seed = str(input("Enter the seed of current time recorded: "))
seed = datetime.strptime(stored_seed, '%Y-%m-%d-%H-%M-%S')
print(seed)
def randExponential(rateLambda):
random.seed(seed)
print(random.random())
return -math.log(1.0 - random.random()) / rateLambda
print(randExponential(5))
When I tried to test this in the console with just numbers, it seems okay so not sure why I'm having trouble when using the datetime library.
>>> random.seed(3)
>>> random.random()
0.23796462709189137
>>> random.seed(3)
>>> random.random()
0.23796462709189137
>>>
Aucun commentaire:
Enregistrer un commentaire