Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Python Random Number Between Range: Step-by-Step Guide

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Open a Python session. Launch the interactive interpreter by typing python (or python3 on macOS/Linux) in a terminal, or open IDLE and start a new file. The examples below work in both.
  2. 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 use from random import randint, but importing the whole module is clearer.
  3. 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 a ValueError.
  4. 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.
  5. 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 like range(1, 10). You can add a step argument, for example random.randrange(0, 100, 5), to get multiples of 5.
  6. 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, use random.sample([1, 3, 5, 7], 2).
  7. 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 a ValueError because 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) or randrange(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 uniform for decimals.
  • Using the random module for security. The random module is not cryptographically secure. For passwords, tokens, or security-sensitive values, use the secrets module instead.

Tips

  • Store your bounds in variables so you can change them in one place: low = 1, high = 100, then random.randint(low, high).
  • To simulate a dice roll, use random.randint(1, 6). For a coin flip, use random.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 uniform and then round: round(random.uniform(1, 10), 2).
  • Use random.choices(population, k=n) when you need multiple picks with replacement, and random.sample when you need unique picks.

Troubleshooting

  • ModuleNotFoundError: No module named 'random'. This is rare because random is built in. It usually means you named a file random.py in your project folder, which shadows the standard library. Rename your file and delete any random.pyc cache.
  • 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 randint or randrange. Convert with int() or use uniform instead.
  • Unexpected distribution. Remember that randint and randrange are uniform over integers; if you need a different distribution, use functions like random.gauss or random.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.