How to Get a Random Number Between a Float Range in Python
Created
Modified
Using uniform Method
The random.uniform(a, b)
method returns a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a.
See the following example:
#!/usr/bin/python3
# Import module
import random
a = random.uniform(1.3, 2.4)
print(a)
1.654179463181758 2.044145546151235
The end-point value b may or may not be included in the range depending on floating-point rounding in the equation a + (b-a) * random().
if you want generate a random float with N digits to the right of point, you can make this:
#!/usr/bin/python3
# Import module
import random
a = random.uniform(1.3, 2.4)
b = round(a, 4)
print(b)
2.2657