[issue27706] Random.seed, whose purpose is purportedly determinism, behaves non-deterministically with strings due to hash randomization

STINNER Victor report at bugs.python.org
Wed Aug 17 08:26:54 EDT 2016


STINNER Victor added the comment:

"Right now the way to work around this is to get some deterministic hash from your string; one mechanism being a truncated SHA256 hash, ..."

It looks like I missed something. Lib/random.py already computes the SHA-512 hash of you pass a string to random.Random constructor?

Using a string as a seed for random.Random already works as expected in Python 3.6:

haypo at selma$ python3 -c "import random; r=random.Random('abc'); print(''.join(map(str, (r.randrange(10) for x in range(10)))), hash('abc'))"
8755240310 6396067846301608395
haypo at selma$ python3 -c "import random; r=random.Random('abc'); print(''.join(map(str, (r.randrange(10) for x in range(10)))), hash('abc'))"
8755240310 -1771227904188177035
haypo at selma$ python3 -c "import random; r=random.Random('abc'); print(''.join(map(str, (r.randrange(10) for x in range(10)))), hash('abc'))"
8755240310 1726464324144904308
haypo at selma$ python3 -c "import random; r=random.Random('abc'); print(''.join(map(str, (r.randrange(10) for x in range(10)))), hash('abc'))"
8755240310 2069899884777593571
haypo at selma$ python3 -c "import random; r=random.Random('abc'); print(''.join(map(str, (r.randrange(10) for x in range(10)))), hash('abc'))"
8755240310 -8244933646981095152
haypo at selma$ python3 -c "import random; r=random.Random('abc'); print(''.join(map(str, (r.randrange(10) for x in range(10)))), hash('abc'))"
8755240310 -3269879388324739111

It was already the case in Python 2.7.

----------
nosy: +haypo

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue27706>
_______________________________________


More information about the Python-bugs-list mailing list