我有一个项目有不同的主文件(不同的模拟)。 当我运行一个主文件时,它应该将种子设置为random(和随机数),项目中的所有模块都应该使用该种子。在
我找不到一个好办法来做这个。我有档案全局.py有了这个:
import random
myRandom=None
def initSeed(seed):
global myRandom
myRandom =random.Random(seed)
那么从一个主要的方面来说:
^{pr2}$然后在主调用的模块中,我会:
from globals import myRandom
但是myRandom在模块中的值为None(即使我在main中修改了它)。为什么,以及如何修复它?有更好的方法吗?在
我将使用一个文件来避免
global
,并将数据和逻辑稍微分开。在种子_处理程序.py
模拟1.py
^{pr2}$模拟2.py
输出:
补遗
我刚在评论中看到这是为了研究。目前,执行simulation1.py会覆盖存储的种子值;这可能是不可取的。可以添加以下功能之一:
正如@jDo在评论中提到的,将
globals.py
重命名为randGlobal.py
。添加了一个用于测试的模块
testResult.py
。在随机全局.py在
在测试结果.py在
^{pr2}$在主.py在
相关问题 更多 >
编程相关推荐