Open In App

Generate Random Float Number in Python

Last Updated : 08 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Generating a random float number in Python means producing a decimal number that falls within a certain range, often between 0.0 and 1.0. Python provides multiple methods to generate random floats efficiently. Let’s explore some of the most effective ones.

Using random.random()

random.random() method random module is the most straightforward way to generate a random float number between 0.0 and 1.0. It’s fast, lightweight and ideal for general-purpose tasks where cryptographic security isn't a concern.

Python
import random
a = random.random()
print(a)

Output
0.8680250182868856

Using numpy.random.random()

numpy.random.random() function is particularly useful when working with numerical arrays or when high performance is required for large-scale data operations. It also generates a float between 0.0 and 1.0 and is optimized for vectorized and scientific computing.

Python
import numpy as np
a = np.random.random()
print(a)

Output
0.25262934628790734

Using secrets.SystemRandom().random()

SystemRandom().random() is a secure alternative to random.random() and is ideal when you need randomness that is safe for security-related applications like password generation or token creation.

Python
import secrets
a = secrets.SystemRandom().random()
print(a)

Output
0.32998191766596596

Using random.uniform(a,b)

random.uniform(a, b) function allows you to generate a random float within any specified range [a, b]. Unlike random.random() which is fixed to the 0–1 range, this is more flexible and useful when you need values outside of that interval, such as simulating random prices, measurements or delays.

Python
import random
a = random.uniform(1, 10)
print(a)

Output
1.6991835366920784



Next Article
Article Tags :
Practice Tags :

Similar Reads