Python Random Number Between Range: Step-by-Step Guide
In this article
To get a random integer between two values in Python, use random.randint(a, b) from the built-in random module. It returns a whole number including both endpoints. For a float, use random.uniform(a, b). For a number that excludes the upper bound, use random.randrange(a, b). No installation is needed because random ships with Python.
What You'll Need
- A working Python 3 installation (any recent version is fine).
- A way to run Python code: the interactive interpreter, IDLE, or a code editor.
- Basic familiarity with importing modules and calling functions.
Step-by-Step
- Open a Python session. Launch the interactive interpreter by typing
python(orpython3on macOS/Linux) in a terminal, or open IDLE and start a new file. The examples below work in both. - Import the random module. At the top of your file or in the interpreter, type
import random. This gives you access to all the functions used below. If you only need one function, you can also usefrom random import randint, but importing the whole module is clearer. - Get a random integer with randint. Call
random.randint(1, 10). This returns an integer from 1 through 10, and both 1 and 10 are possible. The first argument must be less than or equal to the second; otherwise Python raises aValueError. - Get a random float with uniform. Call
random.uniform(0, 1). This returns a floating-point number between 0 and 1, where the endpoints may or may not be included depending on rounding. Use it when you need decimal values rather than whole numbers. - Exclude the upper bound with randrange. Call
random.randrange(1, 10). This returns an integer from 1 through 9, because the upper bound is excluded. It behaves likerange(1, 10). You can add a step argument, for examplerandom.randrange(0, 100, 5), to get multiples of 5. - Pick from a list with choice. If you already have a sequence of allowed values, use
random.choice([1, 3, 5, 7]). This returns one element at random. For multiple unique picks, userandom.sample([1, 3, 5, 7], 2). - Test your code. Run the script or type the calls in the interpreter several times. You should see different results each time. If you always see the same number, check that you are not re-seeding the generator with a fixed value.
Common Mistakes
- Reversing the arguments.
random.randint(10, 1)raises aValueErrorbecause the low bound must not exceed the high bound. Swap them or sort them first. - Forgetting that randint includes both ends. If you want a number from 1 to 9, use
randint(1, 9)orrandrange(1, 10). Mixing these up shifts your range by one. - Using random.seed() in production. Seeding with a fixed value makes the sequence repeatable, which is useful for tests but wrong for games, simulations, or anything that needs unpredictability.
- Assuming randint returns a float. It always returns an integer. Use
uniformfor decimals. - Using the random module for security. The
randommodule is not cryptographically secure. For passwords, tokens, or security-sensitive values, use thesecretsmodule instead.
Tips
- Store your bounds in variables so you can change them in one place:
low = 1,high = 100, thenrandom.randint(low, high). - To simulate a dice roll, use
random.randint(1, 6). For a coin flip, userandom.choice(['heads', 'tails']). - If you need reproducible results for debugging, call
random.seed(42)before generating numbers. Remove it when you are done testing. - For a random float with a specific number of decimal places, generate with
uniformand then round:round(random.uniform(1, 10), 2). - Use
random.choices(population, k=n)when you need multiple picks with replacement, andrandom.samplewhen you need unique picks.
Troubleshooting
- ModuleNotFoundError: No module named 'random'. This is rare because
randomis built in. It usually means you named a filerandom.pyin your project folder, which shadows the standard library. Rename your file and delete anyrandom.pyccache. - ValueError: empty range for randrange(). Your start value is greater than or equal to the stop value. Check the order of arguments.
- Same number every time. Look for a
random.seed()call with a fixed argument, or a loop that reseeds on each iteration. Remove or move the seed call. - TypeError: 'float' object cannot be interpreted as an integer. You passed a float to
randintorrandrange. Convert withint()or useuniforminstead. - Unexpected distribution. Remember that
randintandrandrangeare uniform over integers; if you need a different distribution, use functions likerandom.gaussorrandom.triangular.
Frequently Asked Questions
Does randint include the upper bound?
Yes. random.randint(1, 10) can return 1 or 10. Use randrange(1, 10) if you want to exclude 10.
How do I get a random float between two numbers?
Use random.uniform(a, b). It returns a float in the range [a, b] or [a, b) depending on rounding.
Can I use random for passwords?
No. The random module is not cryptographically secure. Use the secrets module for security-sensitive values.
Why do I get the same number every time?
You are probably calling random.seed() with a fixed value. Remove that call or seed once at the start of your program.
How do I generate a random number without importing random?
You can use secrets.randbelow(n) for integers, but it is designed for security, not general randomness. The random module is the standard choice.
What is the difference between randint and randrange?
randint(a, b) includes both endpoints. randrange(a, b) excludes the upper bound and supports a step argument.