Generate Random Float Numbers in Python

From this tutorial, you’ll learn how to generate random float numbers in Python. We’ll show you ways to produce random float number between 0-1 and also from any float range given as an input.

You can also watch out a detailed post here on Generating random numbers in Python.

Moreover, we will also provide you a simple technique to generate a secure random float number. After that, there is an example to make a random array of float numbers that utilizes the numpy’s random package.

Generate Random Float Numbers


generate random float numbers

Random float number between 0-1

Python random module provides the random() method which you can utilize to generate a random float number. This method calculates the next random floating point number starting from o. 1 to 1.0.

Here is a simple example to explain how it works.

Example

import random

print("First Random float number: ", random.random())
print("Second Random float number: ", random.random())
print("Third Random float number: ", random.random())
print("Fourth Random float number: ", random.random())

Output

First Random float number:  0.8986651512213681
Second Random float number:  0.4571640303921852
Third Random float number:  0.18809066014933495
Fourth Random float number:  0.3611735431028671

Note: The random() method can only serve random float numbers between the range 0.1. To 1.0.

Random float number from a float range

The random module has another useful method which is uniform(). It takes two parameters to specify from where to being and where to end.

Its syntax is as follows:

random.uniform(start, stop)

It returns a RFPN, i.e., N such that start <= N <= stop.

From this function, you can produce a random float number between any range such as 1 to 100 and vice-versa. It takes both ranges the same.

Note: The uniform algorithm may or may not consider last value, i.e., stop and would depend on the floating-point rounding mechanism.

Here is an example with a variety of inputs so that you can get more clarity of this function.

Example

import random

print('Random float number from given range ({} - {}): {}'.format(3.0, 30.0, random.uniform(3.0, 30.0)))
print('Random float number from given range ({} - {}): {}'.format(50.0, 100.0, random.uniform(50.0, 100.0)))
print('Random float number from given range ({} - {}): {}'.format(9, 99, random.uniform(9, 99)))
print('Random float number from given range ({} - {}): {}'.format(9, 99, random.uniform(9, 99)))
print('Random float number from given range ({} - {}): {}'.format(99, 49, random.uniform(99, 49)))
print('Random float number from given range ({} - {}): {}'.format(99, 49, random.uniform(99, 49)))

Output

Random float number from given range (3.0 - 30.0): 5.679656379275675
Random float number from given range (50.0 - 100.0): 54.45378839418061
Random float number from given range (9 - 99): 22.478842526555113
Random float number from given range (9 - 99): 85.10259329700018
Random float number from given range (99 - 49): 60.39557036983186
Random float number from given range (99 - 49): 58.13428190995036

You can check the above output and see that we’ve got different results for successive calls of uniform() method between 9 to 99 or 99 to 49.

Secure random float number

None of the random number generation methods we discussed so far are 100% safe. They all are standard random number generators.

To produce a secure random value, you need to specify the random.SystemRandom() class.

Example

import random

rand_init = random.SystemRandom()
secure_val = rand_init.random()
print("My secure random float number: ", secure_val)
secure_val = rand_init.uniform(24.6, 67.3)
print("My next secure random float number: ", secure_val)

Output

My secure random float number:  0.6513270694572083
My next secure random float number:  45.59338129096113

Generate random float numbers using numpy

NumPy is an external Python package for large data manipulation. It empowers you to generate and utilize the big floating point numbers.

Install NumPy

Run the following shell command in your terminal to install the NumPy package.

# Install the NumPy package
pip install numpy

This module has several methods to produce a random array of float numbers at runtime. But first, you need to add the NumPy using the Python’s pip command.

Here are some cool tips which are utilizing the numpy package and its functions.

Generate a simple random float number

import numpy

random_num = numpy.random.randn()
print("NumPy: Random float number: {}".format(random_num))

random_num = numpy.random.randn()
print("NumPy: Random float number: {}".format(random_num))

Output

NumPy: Random float number: -0.20568236367553763
NumPy: Random float number: 0.3334413761800634

A random float number from any range

import numpy

x = 24.7
y = 79.8

random_num = numpy.random.uniform(x, y)
print("NumPy: Random float number from ({}, {}): {}".format(x, y, random_num))

random_num = numpy.random.uniform(x, y)
print("NumPy: Random float number from ({}, {}): {}".format(x, y, random_num))

Output

NumPy: Random float number from (24.7, 79.8): 66.04576923195697
NumPy: Random float number from (24.7, 79.8): 43.21923755475386

Create an array of float numbers between 0-1

and populate it with random samples from a uniform distribution over [0, 1).

import numpy

numpy_float_arr = numpy.random.rand(5, 5)
print("NumPy: Random float array ***")
print(numpy_float_arr)

Output

NumPy: Random float array ***
[[0.56763831 0.5456857  0.77826188 0.84339491 0.34430262]
 [0.44870468 0.72040306 0.64681969 0.34498156 0.36537689]
 [0.0100336  0.67036109 0.09783366 0.94513373 0.15806453]
 [0.24101949 0.29660109 0.77374996 0.36125114 0.11296157]
 [0.87267528 0.96729392 0.36265287 0.02983375 0.81127663]]

Create an array of float numbers from any range

import numpy

x = 34.8
y = 155.3
m = 5
n = 5

numpy_float_arr = numpy.random.uniform(x, y, size=(m, n))
print("NumPy: Random float array from ({}, {}) of size ({}, {}) ***".format(x, y, m, n))
print(numpy_float_arr)

Output

NumPy: Random float array from (34.8, 155.3) of size (5, 5) ***
[[146.51584692  47.8683704  109.74641911  76.71030277 103.241513  ]
 [ 39.86646662  94.1047315   95.72403306  91.73649989 118.53429242]
 [ 97.52907021  99.62018664 148.64464245 121.19452395  65.60186237]
 [ 52.34066487 132.41908261  42.49632789  61.43665295 108.2545217 ]
 [138.55650981  70.25051053 131.62429029  49.31621039  56.05783395]]

For your information, we’ve used Python format() function for output formatting in this tutorial. If you like to understand how it works, then read the below post.

String Formatting in Python