Python Glossary Русский

all Python 3randomterm

random.uniform

floats

Returns a random floating-point number from the range [a, b], uniformly distributed.

Syntax

random.uniform(a, b)

Whether the endpoint b actually shows up is not guaranteed: the value is computed as a + (b - a) * random(), and float rounding decides, so do not rely on the range being closed. Argument order does not matter — uniform(10, 1) behaves like uniform(1, 10). The result is always a float; for a random integer reach for randint or randrange rather than int(uniform(...)).

Examples

import random
random.seed(1)
print(random.uniform(1, 10))  # → случайное в [1,10]
print(random.uniform(-1, 1))  # → в [-1,1]
print(random.uniform(0, 0.5)) # → в [0,0.5]
print(random.uniform(100, 200))  # → в [100,200]
results = [random.uniform(0,1) for _ in range(5)]
print([round(x,3) for x in results])

See also

Open in the full glossarydocs.python.orgReport an inaccuracy