jeudi 3 novembre 2016

python random seeding is deterministic, but it shouldn't be

From the manual:

random.seed([x])

Initialize the basic random number generator. Optional argument x can be any hashable object. If x is omitted or None, current system time is used; current system time is also used to initialize the generator when the module is first imported. If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom() function for details on availability).

If a hashable object is given, deterministic results are only assured when PYTHONHASHSEED is disabled.

user@MacBook:~$ python
Python 2.7.11 (default, Aug  6 2016, 02:11:50) 
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> r = random.Random('somelongstring' * 40)
>>> r.randint(1, 1000)
332
>>> exit()

user@MacBook-Pro:~$ python
Python 2.7.11 (default, Aug  6 2016, 02:11:50) 
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> r = random.Random('somelongstring' * 40)
>>> r.randint(1, 1000)
332
>>> 

The trouble is, i'm trying to get the same result everytime, but according to the manual and the PYTHONHASHSEED, i shoundn't be getting the same results every time. I've tried this on mac os, linux, python2.7 and pypy and they keep the same results over and over again.




Aucun commentaire:

Enregistrer un commentaire