Python Generate Random Float in Range

How-to basics · Updated November 23, 2026

To generate a random float in a specific range in Python, use random.uniform(a, b), which returns a random float N such that a <= N <= b. For example, random.uniform(1.5, 9.5) gives a float between 1.5 and 9.5 inclusive of both endpoints (in most cases).

Why Use random.uniform()

Python’s random module is the standard library for pseudo-random number generation. The uniform() function is specifically designed for continuous distributions over a given interval. Unlike random.random() which always returns a float in [0.0, 1.0), uniform() scales that result to any range you specify. It uses the formula a + (b - a) * random(), so the lower bound is inclusive and the upper bound is inclusive in the sense that the result can equal b only if random() returns exactly 1.0, which is extremely rare but theoretically possible.

Basic Usage Examples

Here are common ways to use uniform():

  • Random float between 0 and 1: random.uniform(0, 1) – equivalent to random.random().
  • Random float between 10 and 20: random.uniform(10, 20).
  • Negative ranges work fine: random.uniform(-5, 5).
  • If a is greater than b, it still works; the result will be between b and a. For example, random.uniform(10, 5) returns a float in [5, 10].

To use it, import the module first: import random. Then call the function. Here’s a complete snippet:

import random
random_float = random.uniform(2.5, 7.5)
print(random_float)  # e.g., 4.182736455

Alternatives: random.random() and random.gauss()

Sometimes you don’t want a uniform distribution. Python offers other functions:

  • random.random() – returns a float in [0.0, 1.0). To get a range, multiply and add: min + (max - min) * random.random(). This is essentially what uniform() does internally, but writing it manually gives you more control over inclusivity.
  • random.gauss(mu, sigma) – returns a float from a normal (Gaussian) distribution. The first argument is the mean, the second is the standard deviation. This is not bounded to a range; values can fall outside, but most lie within a few standard deviations.
  • random.triangular(low, high, mode) – returns a float in [low, high] with a triangular distribution, where mode is the most likely value.

For most “random float in range” needs, uniform() is the simplest and clearest choice.

How to Choose the Right Range and Distribution

The choice of range and distribution depends on your use case:

  • Simulation (e.g., physical processes) often needs uniform values over a physical range – use uniform().
  • Monte Carlo methods typically use uniform or normal distributions. If you need a bounded uniform, uniform() is fine.
  • Machine learning initialization often uses small random floats, e.g., uniform(-0.05, 0.05).
  • Game development might use uniform() for random spawn positions or damage values.

Always consider whether the upper bound should be inclusive. In practice, the difference is negligible because the probability of hitting exactly the upper bound is astronomically small for floating-point numbers.

Common Pitfalls and Misconceptions

One common mistake is confusing random.randint() with uniform(). randint(a, b) returns an integer, not a float. If you need a float, use uniform() or random.random() scaled.

Another misconception: random.uniform(a, b) does not guarantee that the result is strictly between a and b – it can equal a (if random() returns 0.0) and can equal b (if random() returns 1.0). For most applications, this is irrelevant, but if you need to exclude endpoints, you can use a + (b - a) * random.random() which excludes 1.0, so the upper bound is never returned.

Also, random.uniform() is not cryptographically secure. For security-sensitive applications (e.g., generating passwords or tokens), use secrets module. secrets.randbelow() works with integers; for floats, you can use secrets.randbits() and scale, but it’s more complex.

Performance and Efficiency

Generating random floats is fast – typically under a microsecond. If you need many random numbers, consider using random.choices() or list comprehensions. For large-scale simulations, you might use NumPy’s numpy.random.uniform(), which is vectorized and much faster for arrays. Example:

import numpy as np
arr = np.random.uniform(0, 1, 1000)  # array of 1000 floats

But for single values or moderate loops, the standard library is sufficient.

Seeding for Reproducibility

To make your random sequence reproducible, call random.seed(value) before generating. This is crucial in testing and debugging. For example:

import random
random.seed(42)
print(random.uniform(0, 10))  # always gives the same result

This works across runs on the same Python version, but not necessarily across Python versions or platforms.

Frequently Asked Questions

How do I generate a random float between 0 and 1?

Use random.random() which returns a float in [0.0, 1.0). Or random.uniform(0, 1) which is slightly different because it can return 1.0 (though extremely unlikely).

Can I generate a random float from a normal distribution with a specific mean and standard deviation?

Yes, use random.gauss(mu, sigma). For example, random.gauss(0, 1) gives a standard normal variate. Note that it’s not bounded.

Is random.uniform() inclusive of the upper bound?

In theory, yes, because the underlying random() can return 1.0 (though the probability is about 2^-53). If you need to exclude the upper bound, use a + (b - a) * random.random().

What’s the difference between random.uniform() and random.triangular()?

uniform() gives every value in the range equal probability. triangular() gives a distribution that peaks at a specified mode, so values near the mode are more likely.

How do I generate a random float in Python 3.9 or earlier?

The same way – random.uniform() has been available since Python 1.5. No version-specific changes affect this function.

Can I generate a random float without importing the random module?

No, you need the random module (or another library like NumPy). There is no built-in function that generates random floats without an import.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.