Python Glossary Русский

all Python 3randomfunction

random.setstate

RNG state

Restores the generator's internal state from an object obtained earlier via getstate, so generation resumes from the same point.

Syntax

random.setstate(state)

The state object is an opaque tuple tied to the current generator implementation, so do not persist it and feed it to another Python version — remembering the seed is the safer way to get reproducibility. Also note that setstate rewinds the module-wide generator, so every consumer of random, including third-party libraries, is rewound with it; use a private random.Random() instance when you need isolation.

Examples

import random
random.seed(1)
saved = random.getstate()
first = random.random()
random.setstate(saved)
print(random.random() == first)   # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy