[英]How to generate independent identically distributed (iid) random variables in python
I am developing a simulation infrastructure with some random events (for instance, sources that generate output with a certain probability). 我正在开发具有一些随机事件(例如,以一定概率生成输出的源)的模拟基础结构。 So far I've been doing it using the random.random() function.
到目前为止,我一直在使用random.random()函数进行此操作。 Ex:
例如:
class source:
def output(self, x):
if(random.random()<=x):
return foo
a = []
for i in xrange(10)
a.append(source())
for i in xrange(1000):
for j in xrange(len(a)):
a[j].output()
From what I understand, all of the sources in my list "a" will get random numbers from the same pseudo-random LFSR source, so a[0] will get a sample, then a[1] will get the next one, then a[2], etc. If random.ramdom() generated a truly random sequence, I believe this would still generate 10 iid subsets of values, however, since I am assuming that python uses an LFSR, or a similar scheme, where each subsequent sample depends on the previous sample, taking several subsets of these samples may or may not be independent and identically distributed. 据我了解,列表“ a”中的所有源都将从同一伪随机LFSR源中获取随机数,因此a [0]将获得样本,然后a [1]将获得下一个样本,然后a [2]等。如果random.ramdom()生成了一个真正的随机序列,我相信它仍然会生成10个iid值子集,但是,因为我假设python使用LFSR或类似的方案,其中每个后续样本取决于先前的样本,这些样本的几个子集可能独立也可能不独立且分布均匀。
I have two questions: 我有两个问题:
I looked at other stack overflow posts, like this one for example: Generate multiple independent random streams in python but they don't answer my question. 我查看了其他堆栈溢出帖子,例如这样的帖子: 在python中生成多个独立的随机流,但它们没有回答我的问题。
The Python stdlib random
module is implemented using aa Mersenne Twister. Python stdlib
random
模块是使用Mersenne Twister实现的。 From the docs for random
: 从文档
random
:
Python uses the Mersenne Twister as the core generator.
Python使用Mersenne Twister作为核心生成器。 It produces 53-bit precision floats and has a period of 2**19937-1.
它产生53位精度的浮点数,周期为2 ** 19937-1。
I believe this satisfies your independence requirement. 我相信这可以满足您的独立性要求。 Check out the Wikipedia article, in particular the section on the " k -distribution" property.
请查阅Wikipedia文章,特别是有关“ k -distribution”属性的部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.