How to use the random uniform() method in Python
Overview
The random.uniform() method in Python is used to return a random floating-point number that is greater than or equal to the specified low boundary, and less than or equal to the specified high boundary.
Syntax
random.uniform(a, b)
Parameters
| Parameter | Description |
|---|---|
a |
a is the number that specifies the lowest boundary the return value should have; this parameter is required. |
b |
b is the number that specifies the highest boundary the return value should have; this parameter is required. |
Example
Let’s look at a very simple way to use the random.uniform() method. Let us write a code to generate a random float number starting from 5 and ending at 10.
import randomprint(random.uniform(5,10))
Explanation
- Line 1: We import the
randommodule. - Line 3: We use the
random.uniform()method to return a random float number starting from 5 and ending at 10.