Programmers frequently need to generate random floating-point numbers for a variety of purposes, including game development and software testing. In addition to Python’s powerful standard library, there are numerous third-party libraries available that provide a variety of methods for generating random floating-point numbers.
This guide explores the various methods for generating random floating-point numbers in Python, covering the built-in random and secrets modules, as well as the third-party numpy library.
Using the random Module
The random module is the most commonly used when it comes to generating random numbers in Python. It provides several methods that can be used for generating random floating-point numbers:
random.random()
The random() method returns a random floating-point number between 0.0 and 1.0 (0.0 is inclusive while 1.0 is exclusive). The method doesn’t accept any parameters.
# Return random number between 0.0 and 1.0:
import random
print(random.random())
# Possible output: 0.58297449444842
print(random.random())
# Possible output: 0.43351443489540376
print(random.random())
# Possible output: 0.8000204571334277
random.uniform()
The uniform(a, b) method returns a random floating-point number within a specified range.
# Get a random floating-point number between 1 and 10
print(random.uniform(1, 10))
# Possible output: 8.77786425542658
# Get a random floating-point number between -10 and 10
print(random.uniform(-10, 10))
# Possible output: -1.6196726867798645
Even if you swap the order of a
and b
, the uniform()
method will still work.
# The following two calls are equivalent
print(random.uniform(1, 10))
print(random.uniform(10, 1))
Please keep in mind that the uniform()
method generates a random float between a
and b
using the equation a + (b-a) * random()
. Due to floating-point rounding, the end-point value b
may or may not be included in the range.
Other Methods
The random module also contains several methods designed to generate random floating-point numbers corresponding to a variety of statistical distributions.
triangular() | Returns a random floating-point number based on the Triangular distribution |
betavariate() | Returns a random floating-point number between 0 and 1 based on the Beta distribution |
expovariate() | Returns a random floating-point number based on the Exponential distribution |
gammavariate() | Returns a random floating-point number based on the Gamma distribution |
gauss() | Returns a random floating-point number based on the Gaussian distribution |
lognormvariate() | Returns a random floating-point number based on a log-normal distribution |
normalvariate() | Returns a random floating-point number based on the normal distribution |
vonmisesvariate() | Returns a random floating-point number based on the von Mises distribution |
paretovariate() | Returns a random floating-point number based on the Pareto distribution |
weibullvariate() | Returns a random floating-point number based on the Weibull distribution |
Using the secrets Module
The random module in Python uses algorithms known as pseudo-random number generators (PRNGs) to generate number sequences that appear random. However, it’s important to note that the numbers generated by this module are not cryptographically secure. They are determined by an initial seed value, which means that if the seed is known, the sequence is completely predictable. Therefore, for applications where security is critical, the secrets module should be used instead.
The secrets module is designed for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets.
The secrets module offers several methods for generating random integers, but not floating-point numbers. However, you can use these integer-generating functions and perform additional steps to convert the resulting integers to floating-point numbers.
secrets.randbelow()
The secrets.randbelow(n)
method basically returns a random integer between 0
(inclusive) and n
(exclusive). To get a random float within that range, you would need to scale and shift the output accordingly.
import secrets
# Generate a random integer below 100 and then divide it to get a float
print(secrets.randbelow(100) / 100)
# Possible output: 0.78
The code above uses the randbelow()
method to generate a random integer between 0 and 99 and then divides that number by 100 to create a random floating-point number between 0.0 and 0.99.
secrets.randbits()
The secrets.randbits(k)
method returns an integer with k
random bits.
import secrets
# Generate a random float using randbits by creating a binary fraction
print(secrets.randbits(8) / 2**8)
# Possible output: 0.76171875
The code above generates a random floating-point number between 0.0 and 1.0. It does this by using secrets.randbits(8)
to create a 8-bit random integer and then dividing that integer by 2**8
to normalize it to a floating-point number in the [0, 1)
range.
Using the numpy Library
NumPy is a library for numerical computations in Python that also provides methods for generating random floating-point numbers.
numpy.random.rand()
The numpy.random.rand(shape)
method creates an array of the given shape
and populates it with random samples from a uniform distribution over [0, 1)
. If no argument is given a single Python float is returned.
import numpy as np
# Generate a single random float
print(np.random.rand())
# Possible output: 0.5517333087659516
# Generate an array of random floats
print(np.random.rand(3,2))
# Possible output:
# [[0.98148607 0.50406344]
# [0.94871023 0.49525261]
# [0.09816521 0.97065385]]
numpy.random.uniform()
The numpy.random.uniform(low, high, shape)
method creates an array of defined shape
with random floats within a specified range [low, high)
.
import numpy as np
# Generate a single random float between 2.0 and 5.0
print(np.random.uniform(2.0, 5.0))
# Possible output: 2.5412341969148415
# Generate an array of random floats between 2.0 and 5.0
print(np.random.uniform(2.0, 5.0, 5))
# Possible output: [4.50256363 4.6447651 4.77483119 3.3674158 3.21981209]