mercredi 4 janvier 2023

Python Random Module sample method with setstate notworking

I am wondering if I am doing it wrong when setting the random seed and state. The random number generated from the random.sample seems not predictable. Does anyone know why? Thanks.

>>> state = random.getstate()
>>> random.seed(7)           
>>> x = list(range(10))
>>> random.sample(x, 5)
[5, 2, 6, 9, 0]
>>> random.sample(x, 5)
[1, 8, 9, 2, 4]
>>> random.sample(x, 5)
[0, 8, 3, 9, 6]
>>> random.setstate(state)
>>> random.sample(x, 5)    
[3, 1, 9, 7, 8]
>>> random.sample(x, 5)
[4, 2, 7, 5, 0]
>>> random.sample(x, 5)
[9, 6, 7, 8, 0]



Aucun commentaire:

Enregistrer un commentaire