Are Seeded Random Numbers Not Consistent Between Python Versions?
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)
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.
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module