Ad

Are Seeded Random Numbers Not Consistent Between Python Versions?

- 1 answer

Question:

Why are seeded random outputs seemingly different across python versions? Have I missed something obvious, is it a documented behavior (I could not find it)

Observed results:

An attempt to reproduce seeded random numbers across python versions produces different results:

# python 3.7
rachel = sorted(Random('rachel').sample(range(57), 6))
larry = sorted(Random('larry').sample(range(57), 6))
armin = sorted(Random('armin').sample(range(57), 6))
laura = sorted(Random('laura').sample(range(57), 6))
rachel, larry, armin, laura

output:

([8, 22, 27, 35, 45, 47],
 [10, 18, 20, 29, 45, 47],
 [4, 7, 15, 22, 47, 52],
 [5, 8, 37, 40, 50, 55])

Comparison point:

Whereas a screenshot from Raymond Hettinger's advanced python at Europycon 2011 is showing a different output - probably python 2.6 or 2.7: (the image quality is poor, but the results are clearly different)

enter image description here

Ad

Answer

Looking into the documentation for the seed function in the random module you see that there is a note that in Python 3.2 a different version is used by default.

With version 2 (the default), a str, bytes, or bytearray object gets converted to an int and all of its bits are used.

With version 1 (provided for reproducing random sequences from older versions of Python), the algorithm for str and bytes generates a narrower range of seeds.

This seems enough to answer the fact of different sequences between the screenshot and your case.

Ad
source: stackoverflow.com
Ad