all Python 3randomfunction
random.getstate
RNG state
Return the generator's internal state; restore it later with setstate to replay the sequence.
Syntax
state = random.getstate()
The returned object is opaque — the raw Mersenne Twister state. Save it (pickle works) and hand it back whole to setstate; never pick it apart, and do not count on a state written by a different Python version loading. For ordinary reproducibility seed() is enough; getstate earns its keep when you need to jump back into the middle of a sequence without replaying it. Remember too that the module-level functions all share one generator, so restoring a state affects every other piece of code in the process.
Examples
import random
random.seed(1)
state = random.getstate()
a = random.random()
random.setstate(state)
print(a == random.random()) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy