Smart Systems for Home

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

Python Random Number: Step-by-Step Guide

· 5 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 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 random module 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 python command.
  • A text editor if you prefer saving scripts as .py files.

Step-by-Step

  1. Open your Python environment. Launch IDLE, open a terminal and type python (or python3 on macOS and Linux), or create a new file in your editor. You should see the >>> prompt if you are in interactive mode.
  2. Import the random module. Type import random and press Enter. This loads the module into memory. If you skip this, every function call below will raise a NameError.
  3. 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).
  4. Print the result. Type print(number) or print(random.randint(1, 10)) directly. In interactive mode the value appears immediately; in a script you need the print() call to see it.
  5. 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, use random.uniform(1.5, 4.5). For a number that steps by a value other than 1, use random.randrange(0, 100, 5), which returns 0, 5, 10, and so on up to 95.
  6. 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.
  7. Run the script. Save the file as random_number.py and run it with python random_number.py. Each run should produce a different value.
  8. 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() without import random raises NameError: name 'randint' is not defined. Always import first.
  • Assuming randint excludes the upper bound. Unlike range(), random.randint(1, 10) can return 10. If you want an exclusive upper bound, use random.randrange(1, 10).
  • Confusing random() with randint(). random.random() takes no arguments and returns a float between 0.0 and 1.0. Passing arguments to it causes a TypeError.
  • Using the wrong function for decimals. random.randint(1.0, 5.0) fails because randint expects integers. Use random.uniform(1.0, 5.0) for floats.
  • Expecting cryptographic security. The random module is fine for games, simulations, and sampling, but it is not suitable for passwords, tokens, or security keys. Use the secrets module 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 and random.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, use random.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 than random.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.