Python Generate Random Float Number
Last Updated :
17 May, 2024
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
Similar Reads
Generating random number list in Python
In Python, we often need to generate a list of random numbers for tasks such as simulations, testing etc. Pythonâs random module provides multiple ways to generate random numbers. For example, we might want to create a list of 5 random numbers ranging from 1 to 100. This article explores different m
3 min read
How to generate a random letter in Python?
In this article, let's discuss how to generate a random letter. Python provides rich module support and some of these modules can help us to generate random numbers and letters. There are multiple ways we can do that using various Python modules. Generate a random letter using a string and a random
1 min read
Generate a List of Random Numbers Without Duplicates in Python
There are a few ways to generate a list of random numbers without duplicates in Python. Letâs look at how we can generate a list of random numbers without any duplicates. Using random.sample()This random.sample() method allows us to generate a list of unique random numbers in a single step. [GFGTABS
2 min read
Python - Get Random Range Average
Given range and Size of elements, extract random numbers within a range, and perform average of it. Input : N = 3, strt_num = 10, end_num = 15 Output : 13.58 Explanation : Random elements extracted between 10 and 15, averaging out to 13.58. Input : N = 2, strt_num = 13, end_num = 18 Output : 15.82 E
5 min read
How to Iterate Float List in Python
We are given a list of floats and our task is to iterate the list of floats and print the result. In this article, we will see how to iterate the float list in Python. Example: Input: float_list = [3.14, 2.718, 1.618, 0.707]Output: Using for loop:3.142.7181.6180.707Explanation: Here, we are iteratin
3 min read
Python - Generate Random String of given Length
Generating random strings is a common requirement for tasks like creating unique identifiers, random passwords, or testing data. Python provides several efficient ways to generate random strings of a specified length. Below, weâll explore these methods, starting from the most efficient. Using random
2 min read
Python - Random Numbers Summation
We need to do summation of random numbers. For example, n = [random.randint(1, 10) for _ in range(5)] it will generate random numbers [4, 8, 9, 4, 6] between 1 to 10 so the resultant output should be 29. Using random.randint()random.randint(a, b) function generates a random integer between a and b (
2 min read
Average of Float Numbers - Python
The task of calculating the average of float numbers in Python involves summing all the numbers in a given list and dividing the total by the number of elements in the list. For example, given a list of float numbers a = [6.1, 7.2, 3.3, 9.4, 10.6, 15.7], the goal is to compute the sum of the numbers
3 min read
Python - N Random Tuples list
Sometimes, while working with Python records, we can have a problem in which we need to construct a random tuple list. This can have applications in many domains using gaming and day-to-day programming. Let's discuss specific ways in which this task can be performed in Python. Input: N = 4 R = 6 Out
3 min read
Divide Two Integers to get Float in Python
Python, a versatile and widely used programming language, offers various ways to perform mathematical operations. When it comes to dividing two integers and obtaining a float result, there are multiple approaches to achieve this task. In this article, we'll explore five different methods to divide i
2 min read