lundi 23 mai 2022

Why is python random.seed is not deterministic when passed a non-integer?

The following code on Python 3.8 always returns 18 (I've tried this on 3 different machines):

import random
random.seed(1)
random.randint(1, 100)

However, calling seed with a non-integer Python 3.8 is semi-nondeterministic:

import random
from datetime import date
random.seed(date(2022, 5, 18))
random.randint(1, 100)

I say semi-nondeterministic because if I run the code multiple times within the same Python terminal, I get the same random number. However, but if I restart the Python terminal, I get a different number.

Clearly it's not using id of the object, since that changes every time I make a new date object so I wouldn't get the same result within a Python terminal.

It's also not using hash, since the following (probably) give different results:

random.seed(date(2022, 5, 18))
random.randint(1, 100)

random.seed(hash(date(2022, 5, 18)))
random.randint(1, 100)

What, then, is going on?




Aucun commentaire:

Enregistrer un commentaire