Skip to content

random.random() Function in Python Vijetha Spark By {Examples}

  • by

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.

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

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.

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 parameter.

2.3 Return Value

It returns random float number within 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())

# Output:
# Generated 3 times of random float number:
# 0.013167991554874137
# 0.83746908209646
# 0.25935401432800764

Yields below output.

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

You can use the random.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 random float number will append to the empty list with the help of the list.append() function. Finally, we will get the list of float numbers randomly.

# 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.

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

Sequence of Random number generated depends upon the seeding value of seed() function, which is another function of random module and is used to initialize the random number generator.

If you use the same seed value, you can generate the same set of random number. 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 its syntax and usage 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.

Happy Learning!!

 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. In this article, I will  Read More Python, Python Tutorial, Python Random 

Leave a Reply

Your email address will not be published. Required fields are marked *