Python Glossary Русский

all Python 3randomterm

random.random

floats

Returns a pseudo-random number from [0.0, 1.0), uniformly distributed.

Syntax

random.random()

Under the hood is the Mersenne Twister: the whole sequence can be reconstructed from the generator state, so random() must not be used for passwords, tokens or keys — use the secrets module for that. Exactly 1.0 never comes out (the upper bound is excluded), and values land on a grid of 2**-53 steps, so testing the result for exact equality with a given number is pointless.

Examples

import random
random.seed(42)
print(random.random())  # → 0.6394...
print(random.random())  # → 0.0250...
import random
# Масштабирование на [a, b):
a, b = 5, 10
x = a + (b-a) * random.random()
print(round(x, 2))  # → случайное в [5,10)
print(type(random.random()))  # → float

See also

Open in the full glossarydocs.python.orgReport an inaccuracy