python - 在模拟中实现常见的随机数

标签 python python-2.7 numpy random simulation

我正在用 Python 构建一个小型模拟,我想使​​用 Common Random Numbers以减少变异。我知道我必须实现同步才能使 CRN 工作:

CRN requires synchronization of the random number streams, which ensures that in addition to using the same random numbers to simulate all configurations, a specific random number used for a specific purpose in one configuration is used for exactly the same purpose in all other configurations.

我想知道我想在我的模拟中实现它的方式是否有效,或者我是否应该使用不同的方法。

我的模拟具有三个不同的类别(A 类、B 类、C 类),A 类对象具有随机旅行时间,B 类对象具有随机服务时间和随机使用率,C 类对象具有随机服务时间。当然,每一类对象可以有多个实例。

在模拟开始时,我指定了一个随机数种子 (replication_seed),这样我就可以为每个模拟复制使用不同的种子。

import numpy.random as npr
rep_rnd_strm = npr.RandomState().seed(replication_seed)

然后在每个类的构造函数中,我使用 rep_rnd_strm 生成用于初始化类实例的随机数流的种子:

self.class_rnd_strm = npr.RandomState().seed(rep_rnd_strm.randint(10000000))

然后我使用 self.class_rnd_strm 为类实例所需的每个随机数流生成一个种子。例如 ClassA 的构造函数有:

self.travel_time_strm = npr.RandomState().seed(self.class_rnd_strm.randint(10000000))

而 ClassB 的构造函数有:

self.service_time_strm = npr.RandomState().seed(self.class_rnd_strm.randint(10000000))
self.usage_rate_strm = npr.RandomState().seed(self.class_rnd_strm.randint(10000000))

我在这里所做的是使同步工作的有效方法,还是我应该以不同的方式做事?

最佳答案

是的。这是使其可复制的有效方法,但前提是您可以保证各个类的各个实例的实例化顺序没有随机性。 这是因为如果它们以不同的顺序实例化,那么它们的随机数生成器将获得不同的种子。

关于python - 在模拟中实现常见的随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29012212/

相关文章:

numpy - numpy 数组中标记组件之间的最小边到边欧氏距离

python - 获取文件行数的最快方法

python - 检查字典中是否存在嵌套键的优雅方法?

python - 套接字错误 : [Errno 32] Broken pipe

python - 从句子中查找并选择所需的单词?

python-2.7 - BeamSearch 在 Tensorflow 中花费了很长时间

python - Linux系统上Python的高分辨率时间

python - 替换 Pandas 列中数据框文本中的特定字符串

python - 由于 "perfect separation error",无法运行逻辑回归

arrays - 当我将 numpy 数组作为输入传递给 keras 层时,它具有不同的形状