Generate Random Float Numbers in Python | random() and Uniform()

Python random module is used to generate the random value such as integer, float, etc., in Python. These random values are generated within a particular range. Generating the random float number in Python is accomplished using the “random()” and “uniform()” functions.

In this post, we will provide a detailed guide on how to generate random float numbers in Python using the following methods:

How to Generate Random Float Numbers Using random() Method?

The “random()” method is used to get the random float number between the range “0” to “1”. Let’s see different examples of the “random()” method:

Example 1: Generate a Random Float Number Within the Range (0-1)

The below code is used to generate the float random number:

Code:

import random
num = random.random()
print(num)

The “random()” function generates the random float number between the range “0” and “1”.

Output:

The random float number has been generated successfully.

Example 2: Generate a Random Float Number Specific Decimal Places

The following code generates random float numbers up to specific decimal places:

Code:

import random
print(round(random.random(), 6))                   # 6 decimal places
print(round(random.random(), 2))                   #2 decimal places
print(round(random.random(), 1))                   #1 decimal places

The “round()” function is used to get a random float number up-to specific decimal places by accepting the “random()” function as a first argument and decimal places as a second argument.

Output:

The random float numbers up to specified decimal places have been generated.

Example 3: Generate Random Float Numbers Python List

The below code is utilized to generate the Python list of random float numbers:

Code:

import random
list_1 = []

for item in range(0, 5):
    value = round(random.random(), 2)
    list_1.append(value)
print(list_1)

In the above code:

  • The “random” module and “empty list” has been initialized in the program.
  • The “range()” function is used to create the range for the list elements.
  • The “round()” function is used along with the “random()” function inside the “for loop” to generate the random float numbers up to specific decimal places.
  • The “append()” function is used to append the random float value to empty lists.

Output:

The above output shows the list of random float numbers.

How to Generate Random Float Numbers Using uniform() Method?

The “uniform()” method is used to get the floating point number between two specific values. The following examples are used to generate the random float number: 

Example 1: Generate a Random Float Number Within Specific Range

In the below code, the “random.uniform()” function is used to generate the random float number within the specified range:

Code:

import random
print(random.uniform(0.5, 5.5))
print(random.uniform(5.5, 55.5))
print(random.uniform(2, 95.5))

The “random.uniform()” function takes the lowest possible outcomes and highest possible outcomes as a parameter value.

Output:

The above output generates the random float number within the given range.

Example 2: Generate a Random Float Number Specific Decimal Places

The “round()” function is used with the “random.uniform()” function method to generate the random float numbers up to specific decimal places:

Code:

import random
print(round(random.uniform(10.5, 50.5), 5)) # 5 decimal places
print(round(random.uniform(2.25, 5.5), 2)) # 2 decimal places

The “round()” function returns the floating point values up to specific decimal places by accepting the “random.uniform()” as a first argument and decimal places as a second argument.

Output:

The above output generated the random float numbers up to specified decimal places.

Example 3: Generate Random Float Numbers Python List

The below code generates the list of random float numbers using for loop and “random.uniform()” method:

Code:

import random
list_1 = []

for item in range(0, 5):
    value = round(random.uniform(5.5, 15.5), 2)
    list_1.append(value)
print(list_1)

In the above code:

  • The “random” module is imported into the program.
  • The “range()” function is used to create the range that will represent the total number of list elements.
  • The “round()” function is used along with the “random.uniform()” function to create the random float number of the list within a specific range.
  • The “list.append()” function appends the random float number to an empty list.

Output:

The random float number list has been created successfully.

Conclusion

The “random.random()” function and “random.uniform()” function is used in Python to generate the random float number within the specified range. The “random()” function generates the random float number between the default range (0-1). We can use the “uniform()” function to generate the random float number within any given range. This blog presented an in-depth guide on how to generate random float numbers in Python using the “random()” and “uniform()” functions.