all Python 3randomterm
random
module
Pseudo-random numbers: random integers and floats, picking from a list, shuffling, sampling without repeats.
Syntax
import random
random suits games, simulations and test data, but not passwords or tokens: its generator is predictable, and secrets exists for security. The same seed yields the same sequence, which helps reproducible experiments. Traps: randint(a, b) includes b, unlike randrange and range, and shuffle reorders the list in place and returns None.
Examples
import random
random.seed(42)
print(random.randint(1, 100), random.choice('abc')) # → 82 a
cards = [1, 2, 3]
print(random.shuffle(cards)) # → None — перемешивает на месте
print(sorted(cards)) # → [1, 2, 3]
See also
Open in the full glossarydocs.python.orgReport an inaccuracy