Open In App

Python Generate Random Float Number

Last Updated : 17 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Generating random numbers plays an important role in programming. It can be used in data analysis, cryptography, or even in simulating games like dice rolls, choosing lottery numbers, or creating unpredictable patterns.

When we talk about "float" numbers, we mean numbers with decimals (like 3.14 or 0.5). In this article, we will learn how we can generate a random float number.

Random Float Numbers in Python

The Random Module in Python contains a certain set of functions to help us work with randomness. These functions include generating a random float number between 0 and 1 (but never exactly 1) or letting us specify the range of numbers we want.

Another concept of the Python random module is Seed which lets us set a starting point for the randomness. That's what a "seed" does. It makes the random numbers follow a predictable pattern, which can be helpful for testing or recreating results.

Generate Random Float Numbers in Python

The first step would be to import the random module into the code, so that we can use it's various functions to generate a random number.

import random 

As we know, Python random module provides us with a number of functions to generate a random number, let us see each of these functions one by one with examples.

Random Float Number Between 0 and 1

Python random.random() function is used to generate a number between 0 and 1, but never exactly 1. This function takes no arguments.

random.random()

Example: In this example, we will generate and store a random float number using simple random() function and display its value.

Python
# import random module
import random
# random.random() to generate number 
# between 0 and 1
number = random.random()
print("Your random number is:", number)  

Output:

Your random number is: 0.4351727311439526

Random Float Number in a Range

Python random.uniform() function is used to generate a number between some specific range. It take two arguments, and will generate a number between those two.

random.uniform(a, b)

Example: In this example, we will generate and store a random float number between 10 and 20 using uniform() function and display its value.

Python
# import random module
import random
# random.uniform() to generate number 
# between 10 and 20
number = random.uniform(10, 20)
print("Your random number is:", number)  

Output:

Your random number between 10 and 20 is: 18.693899507434406

Random Float Number with a Seed

The Python random.seed() function ensures that the random function generates the same sequence every time for a particular seed value. It take one argument which is an integer value.

random.seed(a)

Example: In this example, we will generate and store a random float number and see how a seed value affects the number generated. This prints the same random float each time the code is executed for a particular seed value.

Python
# import random module
import random
# random.random() for seed 3
random.seed(3)
print("Your random number for seed 3 is:", random.random())
# random.random() for seed 3
random.seed(5)
print("Your random number for seed 5 is:", random.random())
# random.random() for seed 3
random.seed(3)
print("Your random number for seed 3 is:", random.random())

Output:

Your random number for seed 3 is: 0.23796462709189137
Your random number for seed 5 is: 0.6229016948897019
Your random number for seed 3 is: 0.23796462709189137

Next Article

Similar Reads