Python Glossary Русский

all Python 3randomterm

random.seed

reproducibility

Initializes the pseudo-random number generator. The same seed gives the same sequence.

Syntax

random.seed(a=None)

A classic mistake is calling seed with the same value inside a loop: the generator rewinds every iteration and keeps handing back the identical number. Seed once at startup, and only when you need reproducibility; left alone (seed(None)) the generator pulls entropy from the OS. The docs promise that random() reproduces the same sequence for the same seed, but no such promise covers shuffle or sample across Python versions.

Examples

import random
random.seed(42)
first = [random.random() for _ in range(3)]
random.seed(42)
second = [random.random() for _ in range(3)]
print(first == second)  # → True — то же зерно, та же последовательность
print(round(first[0], 3))  # → 0.639
random.seed('hello')  # зерном может быть и строка
random.seed()  # без аргумента — из системного источника случайности

See also

Open in the full glossarydocs.python.orgReport an inaccuracy