Méthode Python aléatoire setstate()
Exemple
Capturez et restaurez l'état du générateur de nombres aléatoires :
import random
#print a random number:
print(random.random())
#capture the state:
state = random.getstate()
#print another
random number:
print(random.random())
#restore the state:
random.setstate(state)
#and the next random number should be the same
as when you captured the state:
print(random.random())
Définition et utilisation
La setstate()
méthode est utilisée pour restaurer l'état du générateur de nombres aléatoires à l'état spécifié
Utilisez la getstate()
méthode pour capturer l'état
Syntaxe
random.setstate(state)
Valeurs des paramètres
Parameter | Description |
---|---|
state | Required. A state object. the setstate() method will restore the state of the random number generator back to this sate. |