You are currently viewing random.random() Function in Python

Python provides random.random() function from the random module which is used to generate random float number between the range of 0.1 to 1.0(exclude). Almost all module functions depend on the basic function random(). It doesn’t take any argument and returns a random float number within a range of 0.1 to 1.0.

Advertisements

In this article, I will explain random.random() function syntax and usage of how we can generate the random float numbe.

1. Quick Examples of random() Function

If you are in a hurry, below are some quick examples of random() function.


# Below are quick examples of random()

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

# Example 2: Get list of random numbers using random()
list = []
for i in range(4):
   list.append(random.random())

# Example 3: Initialize the random number generator
# and get the same set of random values.
random.seed(3)
print(random.random())

2. random() Function

The random.random() function of a random module in Python 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.

2.1 Syntax of random() Function

Following is the syntax of random() function.


# Syntax of random() function
random.random()

2.2 Parameters

It doesn’t take any parameters.

2.3 Return Value

It returns a random float number within the range of 0 to 1.

3. Get a Random Float Numbers Between 0 to 1

Let’s use random() and get the random float number. Before going to use any function of the random module we need to import the random module of Python.

Related: Generate Random Integers Between 0 and 9 in Python


# Import random module
import random

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

Yields below output.

Python random random

4. Create List of Randoms using Python random() Function

You can also use the Python random() function to generate the list of random float numbers. First, take the empty Python list and iterate the range of the specified number using for loop. For every iteration get random float number and append to the empty list with the help of the list.append() function. Finally, we will get the list of random float numbers.


# Get list of random numbers using random()
# import random
import random
list = []
for i in range(4):
   list.append(random.random())
print("List of random numbers:\n", list)

Yields below output.

Python random random

5. Initialize the random.random() using Seed Value

The sequence of the random number generated depends upon the seeding value of Python seed() function, which is another function of the random module that is used to initialize the random number generator.

If you use the same seed value, you get the same set of random numbers every time you run your program. For example,


import random

# Initialize the random number generator
# Get the same set of random numbers
random.seed(3)
print("Random numbers:")
print(random.random())
print(random.random())
print(random.random())

# Output:
# Random numbers:
# 0.23796462709189137
# 0.5442292252959519
# 0.36995516654807925

6. Conclusion

In this article, I have explained random.random() function of Python and using this syntax and usage of how we can generate the random float number within a range of 0 t0 1. And also explained using this how to generate list of float numbers randomly and finally using seed value to generate the same random values.

Happy Learning!!

References

Related Article