Python Generate Random Float Number

Randomly generated floats within a range are between or equal to the range’s endpoints. A randomly produced float in the range of 1.0 to 3.0, for example, could be 2.9. This tutorial will acquire all the specifics of using the popular Python module called random. It lets you generate random numbers, as its name suggests. Python’s random() part executes pseudo-random number makers for various distributions. For integers, there is indeed a uniform choice from a range. There is a technique to construct a random variation of a list and a function for the random selection process without replacement for categorizations.

Subclass Random and override the random(), seed(), getstate(), and setstate() methods if you wish to utilize a custom basic generator. A new generator can include a getrandbits() method, although it is optional.  It enables randrange() to generate selections over an infinite range. The random module also includes the SystemRandom class, which generates random numbers from operating system sources using the system function os.urandom().

Pseudorandom number generators with various distributions are included in this random module. One commonly used method is random(), which uses a random number generator to generate a value between 0 and 1. Other functions, such as randint(min,max) and randrange, are also available (max). Let’s get started with the random() and uniform() functions of the random module to produce an arbitrary float number in Python.

Example 1:

Now we’ll generate a float number between the value of 1 and 0 at random. Use the random.random() function of the random module to generate a random float number in the semi-open range [0.0, 1.0]. Follow the code below to see where the random module was initially imported. Then, to get a random float number, we used the random function to initialize the variable ‘x.’ Please note that the random() function can only produce float numbers between 0.1 and 1.0. You may also use the uniform() method to produce a random float value between any two values.

import random

a = random.random()

for i in range(2):


Here you can see that random floating-point numbers are successfully generated.

Example 2:

We’ll use the random.uniform() function to generate a random float value inside a range in this example. In Python, the random.uniform() function gives a random floating-point number, and that is within a specified range. For example, it can produce a random float number in the range of 10 to 100. From 50.50 to 75.5, as an alternative. The random.uniform() function returns a random floating-point number N with a start equal to N and stop equal to stop. uniform(10.5, 15.5) generates any float value higher than or equal to 10.5 but less than or equal to 20.5.

The uniform() function takes two arguments (start and stop), all of which are required. You’ll get a TypeError uniform() lacking 1 mandatory positional parameter if you forget any of them. In a float range, the start is the first digit. i.e., the lower bound. If no value is supplied, the default value is 0. The end/last integer in a range is called a stop. It’s the top of the range. There are a few things to keep in mind, such as the fact that the start value does not have to be smaller than the stop value. If start<=stop, a random float number is generated that is larger than or equivalent to the initial number but less than or equal to the stop number. If stop>=start, an arbitrary float number is produced that is larger than or equivalent to the stop number but less than or identical to the start number. The step parameter is not accessible in the random.uniform() method.

import random

print(random.uniform(12.5, 65.5))

print(random.uniform(20, 100))

A random floating-point number is created within a range, as seen below.

Example 3:

Now we’ll create a random float number up to specified decimal places. As illustrated in the examples above, a random float number consists of more than ten decimal points. A random float number with a small number of decimal digits after the decimal point is required in many cases. Use the round() method inside the random.random() and random.uniform() procedures to bound float number length to two decimal digits. We’ve first imported the random module, as shown in the code below. Then, according to the code, we generated several float numbers up to various decimal points.

import random

print(round(random.random(), 3))

print(round(random.uniform(22.22, 44.44), 1))

print(round(random.random(), 2))

print(round(random.uniform(11.11, 77.77), 2))

You can see that float numbers up to 1, 2, and 3 decimal points are created in the output.


We learned about the fundamentals of random number creation in this guide. random.random is a function used for this purpose. The random() function returns the next random float between 0.0 and 1.0. Use the random() method to produce a float number between 0 and 1. We have demonstrated the use of the Python random module to generate random numbers through examples in this Python lesson. With the help of well-detailed example programs, we’ve also taught you how to produce a random floating point integer.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content