You are currently viewing Generate Random Float Numbers in Python

How to generate random float numbers in Python? You can generate random float numbers using uniform() and random() functions of random module. A random() function can generate only float numbers between 0 to 1. Whereas, the uniform() function can generate a random float number between any two specified numbers.

Advertisements

In this article, I will explain these functions and others to generate random float numbers of the specified range.

1. Quick Example Generating Float Number Between a Range

If you are in a hurry, below are some quick examples of generating float numbers.


#  Quick examples of randrange() function.

# Example 1: Get the float random number between 0 to 1
import random
rand_float = random.random()
for i in range(3):
    print(rand_float)

# Example 2: Get the random float number using uniform()
# Intitialize the range limits
a = 10
b = 20
rand_num = random.random()

# Example 3: Generate Random float number of 2 decimal places
rand_float = round(random.random(), 2)

# Example 4: Create an array of random float elements  
ran_arr = np.random.uniform(size = 3, low = 5, high = 10)  

# Example 5: Get random float number from 1 to 10
# using choice and range() function.
rand_int = random.choice(range(1, 10, 2))
rand_float = rand_int/2

# Example 6: Get list of random float numbers
random_list = []
# Set a length of the list to 10
for i in range(4):
    rand_float = random.uniform(10, 20)
    random_list.append(rand_float)

2. Generate a Random Float Numbers Between 0 to 1

You can use a random() function of a random module for generating random numbers within a range of 0 to 1. The number generated is a random value within the range of possible floating-point numbers in Python.

Related: Generate Random Integers Between 0 and 9 in Python


# Get the float random number between 0 to 1
import random
print("Generated 3 times of Random float number:")
for i in range(3):
  print(random.random())

Yields below output.

Python random float

3. Get a Random Float Numbers Between a Range using uniform()

The random.uniform() from a random module is used to generate the random floating number from the given range of specified numbers. It takes lower and upper limit numeric values as its parameters so that a random floating number is generated within this specified range.

Following is the syntax of uniform() function.


# Syntax of uniform() function
random.uniform(a, b)

Let’s pass a specific range of integers(10,20) into random.uniform() to generate a single random floating number within a specific range.


# Import random module
import random

# Get the random float number using uniform()
# Intitialize the range limits
a = 10
b = 20
print("Lower limit:", a)
print("Higher limit:", b)
rand_num = random.uniform(a,b)
print("Random Floating number:", rand_num)

Yields below output.

Python Random float

5. Generate a Random Float Numbers of Two Decimal Places

So far, we have seen random float numbers having decimal places more than 10 places. Now we will see how to get float numbers of two decimal places.

You can customize the random numbers by using the round() function. Use the round() function along with random.random() or random.uniform() function and get the random float numbers of two decimal places.


# Generate Random float number of 2 decimal places
rand_float = round(random.random(), 2)
print("Random float number with 2 decimal places:\n", rand_float)

# Output:
# Random float number with 2 decimal places:
# 0.81

rand_float = round(random.uniform(10.2, 20.2), 2)
print("Random float number with 2 decimal places:\n", rand_float)

# Output:
# Random float number with 2 decimal places:
# 17.72

6. Generate an Array of Random Float Numbers in Python

We can use uniform() function to get the array of random elements. Before going to create a NumPy array of random elements, we need to import the NumPy module, which is one of the liabrary of Python.

First, initialize the random number generator using the seed() function and then specify the size of an array and range with specified numbers and pass them into np.random.uniform() function.


import numpy as np  
np.random.seed(5)  

# Create an array of random float elements  
ran_arr = np.random.uniform(size = 3, low = 5, high = 10)  
print("Random numbers of array is: ", ran_arr)  

# Output:
# Random numbers of array is:  [6.10996586 9.35366153 6.03359578]

7. Get Python Random Float Number with Step

You can use the range() function with the choice() function to generate a random float number. A range() function can generate sequence of numbers with a specific interval and random.choice() function generates a single random element from a specified sequence.

First, pass the range() function into random.choice() and get the random integer from the range of sequences and then, you can get the random float number by converting integer to float.


# Get random float number from 1 to 10
# using choice and range() function.
rand_int = random.choice(range(1, 10, 2))
rand_float = rand_int/2
print("Random float number:", rand_float)

# Output:
# Random float number: 2.5

8. Get Python List of Random Float Numbers

You can get the list of float numbers using the uniform() and the append() function. First, create an empty list then, using the uniform() function get the random float number within a range. Finally, using for loop add a random float number to the empty list using the append() function for every iteration.


import random

# Get list of random float numbers
random_list = []

# Set a length of the list to 10
for i in range(4):
    rand_float = random.uniform(10, 20)
    random_list.append(rand_float)
print(random_list)
    
# Output:
# List of random Float numbers:
# [15.592931823342404, 15.462214061191307, 19.80303716128794, 19.91960806659459]

9. Conclusion

In this article, I have explained using random(), uniform(), and choice() functions of the random module that are used to generate a random float number of the specified range. And also explained using list.append() and for loop how to generate list of random float numbers with examples.

Happy Learning!!

References