![]() |
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 PythonThe 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 PythonThe 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 1Python 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.
Output: Your random number is: 0.4351727311439526 Random Float Number in a RangePython 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.
Output: Your random number between 10 and 20 is: 18.693899507434406 Random Float Number with a SeedThe 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.
Output: Your random number for seed 3 is: 0.23796462709189137 |
Reffered: https://www.geeksforgeeks.org
Python Programs |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |