1

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.

1 Answer 1

3

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.

2
  • I think it's an overkill in practice (not using Mersenne, but rather rejecting the Standard's random), however nice reference to the "k-distribution" of Mersenne.
    – Ami Tavory
    Jun 29, 2015 at 23:27
  • I've edited my answer to hopefully be clearer, what I meant was that the default implementation in Python uses a Mersenne twister
    – maxymoo
    Jun 29, 2015 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.