Python Random Number in Range: Step-by-Step Guide
In this article
To generate a random number in a range in Python, import the random module and call random.randint(start, end) for a whole number, or random.uniform(start, end) for a decimal. Both are part of Python's standard library, so no extra install is needed. The steps below cover the exact syntax, the difference between the two functions, and how to avoid the most common off-by-one and seeding mistakes.
What You'll Need
- Python 3 installed on your computer (any recent 3.x version works; the
randommodule is built in). - A way to run Python code: the interactive interpreter, a saved
.pyfile, or an editor such as IDLE or VS Code. - Basic familiarity with importing a module and calling a function.
Step-by-Step
- Open a Python session. Launch the interactive interpreter by typing
python(orpython3on macOS and Linux) in a terminal, or create a new file namedrandom_demo.pyin your editor. - Import the random module. At the top of your file or at the prompt, type
import random. This loads the standard library module; you do not need pip or any third-party package. - Generate a whole number with randint. Call
random.randint(1, 10). This returns an integer from 1 through 10, and both endpoints are included. Change the two arguments to set your own lower and upper bounds. - Generate a decimal with uniform. Call
random.uniform(1, 10). This returns a floating-point number between 1 and 10. Unlikerandint, the upper bound is not guaranteed to be hit exactly, but the result stays within the range. - Store or print the result. Assign it to a variable, for example
roll = random.randint(1, 6), thenprint(roll)to see the value. Each time you run the code you get a new number. - Pick from a list if that is your goal. Use
random.choice(my_list)to return one random item, orrandom.sample(my_list, k)to returnkunique items. These are handy when your "range" is really a set of options rather than numbers. - Make results repeatable when needed. Call
random.seed(42)before generating numbers to get the same sequence every run. This is useful for tests and tutorials, but leave seeding out of production code where you want genuine randomness.
Common Mistakes
- Assuming the upper bound is excluded.
random.randint(1, 10)can return 10. If you are translating code from another language where the top value is excluded, adjust your arguments. - Swapping the arguments.
random.randint(10, 1)raises aValueErrorbecause the start must be less than or equal to the end. Always put the smaller number first. - Using
random.random()for a range. That function returns a float between 0.0 and 1.0 only. For a custom range, useuniformorrandintinstead. - Calling
random.seed()in a loop. Reseeding repeatedly can produce repeated or predictable values. Seed once at the start, or not at all. - Expecting cryptographic security. The
randommodule is fine for games and simulations but is not suitable for passwords or security tokens. Use thesecretsmodule for those cases.
Tips
- Use
random.randrange(start, stop, step)when you need a step, such as even numbers only:random.randrange(0, 11, 2). - For a quick dice roll,
random.randint(1, 6)is the shortest correct form. - If you only need a float between 0 and 1,
random.random()is faster and simpler thanuniform(0, 1). - Keep the module import at the top of the file so the code is easy to read and reuse.
Troubleshooting
If you see NameError: name 'random' is not defined, you forgot the import random line. If you get ValueError: empty range for randrange(), your start value is larger than your end value; swap them. If the same number appears every run, check for a random.seed() call that is forcing a fixed sequence. If you are working inside a virtual environment and the module seems missing, confirm you are running the same Python interpreter where the standard library is available; the random module ships with Python and does not need installing. Finally, if your code runs but the output type is not what you expected, remember that randint returns an int while uniform returns a float.
Frequently Asked Questions
Does random.randint include the upper bound?
Yes. random.randint(1, 10) can return 1 and can return 10. Both endpoints are inclusive.
How do I get a random float in a range?
Use random.uniform(low, high). It returns a floating-point number between the two values, and the upper bound is not guaranteed to be reached exactly.
Can I generate a random number without importing anything?
No. The random functions live in the random module, so you must import it first. It is part of the standard library, so no download is required.
Why do I get the same number every time?
You likely called random.seed() with a fixed value. Remove the seed call, or seed only once at the start of your program.
Is the random module safe for passwords?
No. Use the secrets module for security-sensitive values such as tokens or passwords. The random module is intended for simulations and games.
How do I pick a random item from a list?
Use random.choice(my_list) for one item, or random.sample(my_list, k) for several unique items.