Search code examples
pythonrandomprobabilityprobability-theory

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. 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.

I have two questions:

  1. What kind of distributions do I actually get using my pseudocode or something similar to that
  2. How do I get several iid random variables in python?

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.


Solution

  • The Python stdlib random module is implemented using a a Mersenne Twister. From the docs for random:

    Python uses the Mersenne Twister as the core generator. It produces 53-bit precision floats and has a period of 2**19937-1.

    I believe this satisfies your independence requirement. Check out the Wikipedia article, in particular the section on the "k-distribution" property.