Python 3 – Number uniform() Method
In Python, the uniform() method is one of the essential functions in the random module. This method is used to generate floating-point numbers uniformly distributed over a specified range. The method takes two parameters, which are the lower limit and the upper limit of the interval. The uniform() method returns a random floating-point number between the two limits.
Syntax
random.uniform(a, b)
Here, a and b are two floating-point numbers that define the interval. The method will return a random floating-point number in the range [a, b].
Let’s check out some examples:
Example 1:
import random
print("Random float number between 1 and 20 ", random.uniform(1, 20))
Output:
Random float number between 1 and 20 11.149300402183908
Example 2:
import random
print("Random float number between 50 and 100 ", random.uniform(50, 100))
Output:
Random float number between 50 and 100 53.24331641965403
Example 3:
import random
print("Random float number between -5 and 5 ", random.uniform(-5, 5))
Output:
Random float number between -5 and 5 -1.7059257708586815
Example 4:
import random
print("Random float number between 0 and 1 ", random.uniform(0, 1))
Output:
Random float number between 0 and 1 0.6935540655367048
Conclusion
The uniform() method is a useful function for generating random floating-point numbers that are uniformly distributed over a specified range. The method takes two parameters, which are the lower limit and the upper limit of the interval. The uniform() method returns a random floating-point number between the two limits. With the help of this article, we hope you have learned what the uniform() method is, how it works, and how you can use it to generate random floating-point numbers in your Python code.