random.uniform
One line of Python: a + (b - a) * random(). So the result is always a float (even for int bounds), reversed bounds just work, and equal bounds return that value.
Common call
random.uniform(1.5, 4.5)
Returns
float between a and b
Replaces
a + (b - a) * random.random()
Watch out
int(uniform(1, 6)) never gives 6: use randint for ints
random.uniform(aa — One end of the range: the value returned when random() gives 0.0.type: float | int · required, bb — The other end. It may be smaller than a.type: float | int · required)
→ float
Demo
Live evaluation
Three draws between a and b from a seeded generator. Try swapping a and b, or making them equal.
Try:
Inputs
seedint | stran int or a string
afloatone end
bfloatthe other end
Code
import random random.seed(3) [random.uniform(1.0, 10.0) for _ in range(3)]
Result
[3.141681643827022, 5.898063027663567, 4.329596498932713]
With seed 3, uniform(1.0, 10.0) and uniform(10.0, 1.0) use the same random() values: 3.141681643827022 and 7.858318356172978 add up to exactly 11, because swapping a and b mirrors the result inside the range. With a == b the width is 0, so every draw is a itself.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| a | float | int | yes | One end of the range: the value returned when random() gives 0.0. |
| b | float | int | yes | The other end. It may be smaller than a. |
Return value
float — N with a <= N <= b (or b <= N <= a when b < a). b itself may or may not occur, depending on rounding.
Common patterns
Random price or measurement
Round for display only; keep the full float for calculations.
import random price = round(random.uniform(9.99, 49.99), 2)
A random point in a rectangle
One uniform per axis.
import random point = (random.uniform(0, width), random.uniform(0, height))
Noise around a value
Symmetric jitter of up to plus or minus 5 percent.
import random noisy = value * random.uniform(0.95, 1.05)
Examples
1. Three floats from 1 to 10
import random
random.seed(3)
[random.uniform(1, 10) for _ in range(3)]
Returns
[3.141681643827022, 5.898063027663567, 4.329596498932713]2. Reversed bounds work too
import random
random.seed(3)
[random.uniform(10, 1) for _ in range(3)]
Returns
[7.858318356172978, 5.101936972336433, 6.670403501067287]3. Exactly a + (b - a) * random()
import random
random.seed(42)
random.uniform(5.0, 10.0)
Returns
8.197133992289424. Int bounds still give a float
import random
random.seed(42)
random.uniform(5, 5)
Returns
5.05. Between -1 and 1
import random
random.seed(42)
[random.uniform(-1, 1) for _ in range(3)]
Returns
[0.2788535969157675, -0.9499784895546661, -0.4499413632617615]6. A price with two decimals
import random
random.seed(3)
round(random.uniform(20.0, 25.0), 2)
Returns
21.197. Bounds must be numbers
import random
random.uniform('1', 5)
Returns
TypeError: unsupported operand type(s) for -: 'int' and 'str'Pitfalls
1. int(uniform(1, 6)) as a die
uniform(1, 6) is a float below 6 (practically never 6.0), so int() gives 1 to 5. Use randint(1, 6) for whole numbers.
int(uniform(1, 6))
import random random.seed(42) [int(random.uniform(1, 6)) for _ in range(10)]
[4, 1, 2, 2, 4, 4, 5, 1, 3, 1]
randint(1, 6)
import random random.seed(42) [random.randint(1, 6) for _ in range(10)]
[6, 1, 1, 6, 3, 2, 2, 2, 6, 1]
2. Printing all 17 digits
A random float shows its full repr. Round when you display it, not before you compute with it.
raw float
import random random.seed(42) random.uniform(0, 100)
63.942679845788376
round(x, 2)
import random random.seed(42) round(random.uniform(0, 100), 2)
63.94
When to use
Use it
- A continuous value in a range: prices, coordinates, durations, noise
- Either order of bounds, e.g. from user input
Reach for something else
- Whole numbers → randint / randrange
- Values clustered around a mean → gauss / normalvariate or triangular
- Exact decimal amounts (money) → randint on cents, then divide
Notes
CPython impl
Lib/random.py: return a + (b - a) * self.random() — plain float arithmetic, so the result is identical on every platform
End point
b can be returned only through rounding of a + (b - a) * random(); the docs say it "may or may not be included"
Types
int arguments are fine (converted by the arithmetic); the result is always a float
FAQ
random.uniform(a, b). It returns a float between a and b in either order of arguments; seed the generator first if you need repeatable values.