Tanmay
Tanmay

Reputation: 41

Unexpected behaviour in python random number generation

I have the following code:

import random

rand1 = random.Random()
rand2 = random.Random()

rand1.seed(0)
rand2.seed(0)

rand1.jumpahead(1)
rand2.jumpahead(2)

x = [rand1.random() for _ in range(0,5)]
y = [rand2.random() for _ in range(0,5)]

According to the documentation of jumpahead() function I expected x and y to be (pseudo)independent sequences. But the output that I get is:

x: [0.038378463064751012, 0.79353887395667977, 0.13619161852307016, 0.82978789012683285, 0.44296031215986331]

y: [0.98374801970498793, 0.79353887395667977, 0.13619161852307016, 0.82978789012683285, 0.44296031215986331]

If you notice, the 2nd-5th numbers are same. This happens each time I run the code.

Am I missing something here?

Upvotes: 4

Views: 679

Answers (2)

Winston Ewert
Winston Ewert

Reputation: 45089

I think there is a bug, python's documentation does not make this as clear as it should.

The difference between your two parameters to jumpahead is 1, this means you are only guaranteed to get 1 unique value (which is what happens). if you want more values, you need larger parameters.

EDIT: Further Explanation

Originally, as the name suggests, jumpahead merely jumped ahead in the sequence. Its clear to see in that case where jumping 1 or 2 places ahead in the sequence would not produce independent results. As it turns out, jumping ahead in most random number generators is inefficient. For that reason, python only approximates jumping ahead. Because its only approximate, python can implement a more effecient algorithm. However, the method is "pretending" to jump ahead, passing two similiar integers will not result in a very different sequence.

To get different sequences you need the integers passed in to be far apart. In particular, if you want to read a million random integers, you need to seperate your jumpaheads by a million.

As a final note, if you have two random number generators, you only need to jumpahead on one of them. You can (and should) leave the other in its original state.

Upvotes: 2

ThiefMaster
ThiefMaster

Reputation: 318768

rand1.seed(0)
rand2.seed(0)

You initialize them with the same values so you get the same (non-)randomness. Use some value like the current unix timestamp to seed it and you will get better values. But note that if you initialize two RNGs at the same time with the current time though, you will get the same "random" values from them of course.

Update: Just noticed the jumpahead() stuff: Have a look at How should I use random.jumpahead in Python - it seems to answer your question.

Upvotes: 4

Related Questions