Python Random Number: Step-by-Step Guide
In this article
To generate a random number in Python, import the random module and call random.randint(a, b) for a whole number between a and b inclusive, or random.random() for a decimal between 0.0 and 1.0. That is the shortest working answer. The rest of this guide covers the exact steps, the differences between the common functions, and the mistakes that trip people up.
What You'll Need
- Python 3 installed on your computer (any recent version works; the
randommodule is part of the standard library, so no extra packages are required). - A way to write and run Python code: IDLE, VS Code, PyCharm, or a terminal with the
pythoncommand. - A text editor if you prefer saving scripts as
.pyfiles.
Step-by-Step
- Open your Python environment. Launch IDLE, open a terminal and type
python(orpython3on macOS and Linux), or create a new file in your editor. You should see the>>>prompt if you are in interactive mode. - Import the random module. Type
import randomand press Enter. This loads the module into memory. If you skip this, every function call below will raise aNameError. - Generate a whole number in a range. Use
random.randint(1, 10). This returns an integer from 1 to 10, and both endpoints are possible. Assign it to a variable if you want to reuse it:number = random.randint(1, 10). - Print the result. Type
print(number)orprint(random.randint(1, 10))directly. In interactive mode the value appears immediately; in a script you need theprint()call to see it. - Choose a different distribution if needed. For a decimal between 0.0 and 1.0, use
random.random(). For a decimal in a custom range, userandom.uniform(1.5, 4.5). For a number that steps by a value other than 1, userandom.randrange(0, 100, 5), which returns 0, 5, 10, and so on up to 95. - Pick a random item from a list. Use
random.choice(["red", "green", "blue"]). This is often more useful than generating a number and indexing manually. - Run the script. Save the file as
random_number.pyand run it withpython random_number.py. Each run should produce a different value. - Make results repeatable when testing. Call
random.seed(42)before generating numbers. The same seed produces the same sequence every time, which is helpful for debugging or for lessons. Remove the seed when you want true unpredictability.
Common Mistakes
- Forgetting to import the module. Calling
randint()withoutimport randomraisesNameError: name 'randint' is not defined. Always import first. - Assuming
randintexcludes the upper bound. Unlikerange(),random.randint(1, 10)can return 10. If you want an exclusive upper bound, userandom.randrange(1, 10). - Confusing
random()withrandint().random.random()takes no arguments and returns a float between 0.0 and 1.0. Passing arguments to it causes aTypeError. - Using the wrong function for decimals.
random.randint(1.0, 5.0)fails becauserandintexpects integers. Userandom.uniform(1.0, 5.0)for floats. - Expecting cryptographic security. The
randommodule is fine for games, simulations, and sampling, but it is not suitable for passwords, tokens, or security keys. Use thesecretsmodule for those cases. - Seeding inside a loop. Calling
random.seed()repeatedly with the same value resets the sequence and can produce identical numbers. Seed once at the start if you need reproducibility.
Tips
- Use
random.randint(a, b)when you need an inclusive range andrandom.randrange(start, stop, step)when you need control over the step or an exclusive upper bound. - For a random sample without repeats, use
random.sample(population, k). For a shuffled copy of a list, userandom.shuffle(list). - If you need the same random sequence on different machines, set the seed to the same integer on each machine.
- Remember that
random.choice()works on any sequence, including strings and tuples, not just lists. - When generating a random integer for a dice roll,
random.randint(1, 6)is clearer thanrandom.randrange(1, 7)because the inclusive range matches the faces of the die.
Troubleshooting
If you see NameError: name 'random' is not defined, the import line is missing or misspelled. Add import random at the top of the file. If you get TypeError: randint() missing 2 required positional arguments, you called randint() without the two bounds. Supply both, for example random.randint(1, 100). If the same number appears every time you run the script, check whether you left a random.seed() call with a fixed value; remove it or change the seed. If you are running the script from a folder with a file named random.py, Python may import that file instead of the standard library module. Rename your file and delete any __pycache__ folder that contains a compiled version of it. If the output is a float when you expected an integer, you used random.random() or random.uniform(); switch to randint or randrange.
Frequently Asked Questions
How do I generate a random number between 1 and 100 in Python?
Use import random then random.randint(1, 100). Both 1 and 100 are possible results.
What is the difference between randint and randrange?
randint(a, b) includes both endpoints. randrange(start, stop, step) excludes the stop value and lets you set a step, so randrange(1, 101) is equivalent to randint(1, 100).
Can I generate a random decimal number?
Yes. random.random() gives a float between 0.0 and 1.0. For a custom range, use random.uniform(low, high).
Is the random module secure enough for passwords?
No. The random module is not cryptographically secure. Use the secrets module for passwords, tokens, and security-sensitive values.
How do I get the same random numbers every time?
Call random.seed(some_integer) before generating numbers. The same seed produces the same sequence on every run.
Why does my script always produce the same number?
Most often a fixed seed is being set, or the script is not actually re-running the generation step. Check for a random.seed() call and remove it if you want different results each time.