Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用random.seed(seed)复制过去的模拟,但由于值不同而出现问题_Python_Random_Simulation_Random Seed - Fatal编程技术网

Python 使用random.seed(seed)复制过去的模拟,但由于值不同而出现问题

Python 使用random.seed(seed)复制过去的模拟,但由于值不同而出现问题,python,random,simulation,random-seed,Python,Random,Simulation,Random Seed,我试图重现我以前运行过的模拟,这样我将当前日期时间的种子记录在一个文本文件中,然后使用记录的日期时间种子获得与以前相同的值 但是,我不确定为什么得出的值与我在上一次模拟中运行的值不相似 这是我尝试运行程序时得到的结果: =================== RESTART: /Users/ivanteong/Desktop/e.py =================== Choose 1 to run simulation based on random seed of current ti

我试图重现我以前运行过的模拟,这样我将当前日期时间的种子记录在一个文本文件中,然后使用记录的日期时间种子获得与以前相同的值

但是,我不确定为什么得出的值与我在上一次模拟中运行的值不相似

这是我尝试运行程序时得到的结果:

=================== 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
>>> 
代码如下

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))
当我试着在控制台中用数字来测试这个问题时,似乎还可以,所以我不确定为什么在使用datetime库时会遇到问题

>>> random.seed(3)
>>> random.random()
0.23796462709189137
>>> random.seed(3)
>>> random.random()
0.23796462709189137
>>> 

您的变量
seed
不是全局变量,因此当您在
randExponential
函数中使用
random.seed(seed)
时,它会传递一个尚未初始化的种子变量,因此它只传递
None
,这是默认值,并使用当前时间。在调用
random.seed(seed)
之前,只需调用
randExponential
,然后去掉函数中的调用,它就会工作,或者您可以将seed传递到函数中

编辑:

由于某种原因,我一直无法找到答案,
datetime.strtime()
函数似乎在每次调用时都会对字符串进行轻微更改,从而创建不同的随机生成,而删除这些会使其正常工作

这是我的密码:

import math
import random
from datetime import datetime


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:
    seed = datetime.strftime(datetime.now(), '%Y-%m-%d-%H-%M-%S')
    print(seed)
    f = open('seed.txt', 'a')
    f.write(str(seed))
    f.write('\n')
    f.close()

elif reproduce == 2:
    seed = str(input("Enter the seed of current time recorded :"))
    print(seed)

def randExponential(rateLambda, seed):
    random.seed(seed)
    print(random.random())
    return -math.log(1.0 - random.random()) / rateLambda

您需要调用
datetime.strtime()
有什么原因吗?出于某种原因,如果我只使用
datetime.strftime()
callI的输出,我需要在运行一次并将种子写入一个文本文件(如代码中所示)后使用存储的时间重新生成模拟,但是如果您只使用种子而不使用
datetime.strftime()
,你能详细说明一下吗?我不明白你怎么能不用种子来复制模拟?或者提出代码的工作变体?