How to generate random float numbers in Python? You can generate float numbers using random.uniform() and random.random() functions of random modules. 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.
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.random() function of a random module is one the most useful function for generating random numbers and is used to generate a random float number within a range of 0 to 1. Let’s use random() and get the random float number.
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.
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.
3.1 Syntax of Random uniform() Function
Following is the syntax of uniform() function.
# Syntax of uniform() function
random.uniform(a, b)
3.2 Parameters
Following are the parameters of uniform() function.
a : (Required) Lower limit of specified range.
b : (Required) Upper limit of specified range.
3.3 Return Value
It returns a random floating number from specified range.
4. Usage of Random uniform() Function
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)
print(“Random Floating number:”, rand_num)
Yields below output.
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() and 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 random.uniform() function we can 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 using these in-built functions we can perform mathematical operations.
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, will return the specified size of the array of random elements within the specified range.
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 range() function with choice() function to generate a random float number. A range() function can generate sequence of numbers with a specific interval and random.choice() function is one of the function of random module and it generates a single random element from a specified sequence such as a list, a tuple, a range, a string, etc.
First, pass the range() function into random.choice() and get the random integer from range of sequence 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() with 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 how we can 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!!
How to generate random float numbers in Python? You can generate float numbers using random.uniform() and random.random() functions of random modules. 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. In this article, I will explain these functions and Read More Python, Python Tutorial, Python Random