Generate a Random Number Between 0 and 1 in Python
In this article
To generate a random number between 0 and 1 in Python, import the random module and call random.random(): import random; number = random.random(). It returns a floating-point value greater than or equal to 0.0 and strictly less than 1.0.
The simplest way with random.random()
Here is a complete example you can run in a Python script or interactive session:
import random
number = random.random()
print(number)
A possible result is 0.374829164, but the value changes when the program runs again. The result is a float, which means it can include a fractional part. Python documents the range as 0.0 ≤ value < 1.0: zero is possible, while exactly 1.0 is not.
The function takes no arguments. In particular, do not write random.random(0, 1); that raises a TypeError. The range is already built into the function.
How to choose a different range
If you need a random float in another interval, scale and shift the value returned by random.random(). For a lower bound a and an upper bound b, use:
value = a + (b - a) * random.random()
This produces a value from a up to, but not including, b, when a is less than b. For example, to get a float from 5 up to (but not including) 10:
import random
value = 5 + (10 - 5) * random.random()
print(value)
Another option is random.uniform(a, b), which is intended to return a random floating-point number between two endpoints:
value = random.uniform(5, 10)
Be aware that floating-point rounding can affect whether the upper endpoint is included in practice. If excluding the upper bound is important, use the scaled random.random() expression and confirm that your application handles floating-point values as intended.
Floats, integers, and rounding
A common source of confusion is the difference between a random float and a random integer. random.random() returns a float, not a whole-number choice. If you want an integer from 0 through 1, use random.randint(0, 1); it returns either 0 or 1, with both endpoints included.
To convert a random float to an integer, avoid simply calling int(random.random()). Since the generated number is always below 1, that expression always returns 0. Likewise, rounding a random float does not create a uniformly distributed choice between zero and one: values below 0.5 round to 0, while values at or above 0.5 round to 1, aside from Python's tie-to-even rounding behavior at exact halfway values.
Choose the function that matches the kind of result you need:
- Use
random.random()for a float in the half-open interval from 0 to 1. - Use
random.randint(0, 1)for an integer choice of 0 or 1. - Use
random.choice([0, 1])when choosing between two values in a collection.
Reproducible results and random seeds
Python's standard random module uses a pseudorandom number generator. Its output is designed to look random for many general-purpose tasks, but it is generated by an algorithm rather than by a source of true physical randomness. That makes it useful for simulations, games, sampling, and randomized tests.
For debugging or repeatable experiments, set a seed with random.seed(). With the same Python implementation, seed, and sequence of calls, the generator can reproduce the same sequence:
import random
random.seed(42)
print(random.random())
Seeding is not a way to make values more random or more secure. It makes a sequence reproducible, which can help when tracking down a bug or comparing a simulation run. Avoid setting the same fixed seed before every draw if you expect successive calls to vary: resetting it repeatedly restarts the sequence.
Security and cryptographic randomness
Do not use random.random() for passwords, account-reset links, authentication tokens, encryption keys, or other security-sensitive values. The standard random module is not designed to resist prediction by an attacker.
For security-sensitive random values, use Python's secrets module, which draws from a source intended for cryptographic use. For example, secrets.randbelow(2) returns either 0 or 1, and secrets.token_urlsafe() can create a URL-safe token. Choose an appropriate token length and follow the requirements of the system using it; merely switching modules does not make an insecure authentication design safe.
Common errors and practical tips
- Forgetting the import: call
import randombefore usingrandom.random(), or Python will report that the namerandomis not defined. - Expecting exactly 1: the function's upper limit is exclusive, so a result of exactly 1.0 is not returned. If your code needs a closed interval, define the desired behavior explicitly rather than assuming the endpoint appears.
- Using a string instead of a number: write
random.random()without quotation marks. Quoting it makes it text, not a function call. - Expecting the same result each run: without a fixed seed, results generally differ between runs. Set a seed only when repeatability is useful.
For a probability check, a simple comparison is often enough. For instance, random.random() < 0.2 is true about 20 percent of the time over many independent draws. It is a probabilistic outcome, not a guarantee that exactly one in every five calls will be true.
Frequently Asked Questions
Does random.random() include 0 and 1?
It can return 0.0, but it does not return 1.0. Its range is 0.0 inclusive to 1.0 exclusive.
How do I generate a random decimal between 0 and 1?
Use import random followed by random.random(). The result is already a floating-point decimal in the range [0, 1).
How can I get a random number between 0 and 1 inclusive?
If you mean a random integer, use random.randint(0, 1). For a float, an inclusive upper endpoint is not the behavior of random.random(); clarify whether your application truly needs that endpoint and select a method with appropriate numerical behavior.
How do I make the random result repeat?
Call random.seed(value) before generating values, using the same seed and call sequence when you want a reproducible pseudorandom sequence.
Is random.random() safe for passwords?
No. Use Python's secrets module for security-sensitive tokens and choices, and design the surrounding authentication or security process carefully.