Smart Systems for Home

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

Python: Generate 10 Random Numbers (Easy 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 10 random numbers in Python, use the random module from the standard library. The simplest approach is a list comprehension: [random.randint(1, 100) for _ in range(10)]. That returns 10 integers between 1 and 100 inclusive. If you need floats, use random.uniform(0, 1); for unique values, use random.sample(range(1, 101), 10). No installation is required because random ships with Python.

What You'll Need

  • Python 3 installed on your computer (any recent version works).
  • A way to run Python code: the interactive interpreter, a saved .py file, or an editor such as IDLE or VS Code.
  • No extra packages. The random module is part of the standard library.

Step-by-Step

  1. Open a Python session. On Windows, open Command Prompt and type python. On macOS or Linux, open Terminal and type python3. You can also create a new file named random_numbers.py in any editor.
  2. Import the random module. Add this line at the top of your file or type it at the prompt:
    import random
    This gives you access to all the random-number functions. If you only need one function, you can write from random import randint instead, but importing the whole module is clearer for beginners.
  3. Decide what kind of numbers you want. Integers, decimals, or unique values? Each has a different function:
    • random.randint(a, b) returns an integer from a to b, including both ends.
    • random.uniform(a, b) returns a float between a and b.
    • random.sample(range(a, b), n) returns n unique integers from the range.
  4. Generate the 10 numbers with a loop or list comprehension. The shortest way is a list comprehension:
    numbers = [random.randint(1, 100) for _ in range(10)]
    print(numbers)
    This creates a list of 10 integers between 1 and 100. The underscore _ is just a throwaway variable name; you can use i if you prefer.
  5. Or use a for loop if you find it easier to read. The loop version does the same thing:
    numbers = []
    for i in range(10):
        numbers.append(random.randint(1, 100))
    print(numbers)
    Both approaches produce 10 numbers. The loop is useful if you want to do something else with each number as it is generated.
  6. Run the code. If you are in the interactive interpreter, press Enter after each line. If you saved a file, run it with python random_numbers.py (or python3 on macOS and Linux). You should see a list of 10 numbers printed to the screen.
  7. Check the output. The numbers will be different every time you run the program because Python seeds its random generator from the system clock by default. If you need the same numbers every run, call random.seed(42) before generating them. The value 42 is arbitrary; any integer works.
  8. Adjust the range if needed. Change the arguments to randint to set your own minimum and maximum. For example, random.randint(0, 9) gives single-digit numbers, while random.randint(1, 1000) gives larger ones. The range is inclusive, so randint(1, 10) can return 10.

Common Mistakes

  • Forgetting to import random. If you see NameError: name 'random' is not defined, you skipped the import line.
  • Using random.random() when you want integers. That function returns a float between 0.0 and 1.0, not a whole number. Use randint or randrange for integers.
  • Expecting unique numbers from randint. A loop with randint can repeat values. If uniqueness matters, use random.sample or random.choices with the appropriate arguments.
  • Off-by-one range errors. randint(1, 10) includes 10, but range(1, 10) stops at 9. Mixing the two can give you a different spread than you intended.
  • Seeding inside a loop. Calling random.seed() repeatedly with the same value produces the same number each time. Seed once, before generating.

Tips

  • Use random.choices(population, k=10) when you want 10 numbers with replacement (repeats allowed) from a specific list.
  • Use random.sample(range(1, 101), 10) when you need 10 distinct numbers. It raises an error if you ask for more unique values than the range contains.
  • For reproducible results in tests or tutorials, call random.seed() with a fixed integer at the start of your script.
  • If you need cryptographically secure random numbers, use the secrets module instead. random is fine for simulations, games, and sampling, but it is not designed for security.
  • Print the numbers with print(*numbers) to see them space-separated instead of as a list with brackets and commas.

Troubleshooting

If your script does nothing, check that you actually called print() or ran the file. A common mistake is defining the list but never displaying it.

If you get ModuleNotFoundError: No module named 'random', you are probably running a stripped-down Python build or have named your own file random.py, which shadows the standard library. Rename your file and delete any random.pyc cache file next to it.

If the numbers look suspiciously similar every run, you may have seeded the generator with a fixed value earlier in the script. Remove or change the seed call.

If random.sample raises ValueError: Sample larger than population, your range is too small for 10 unique values. Widen the range or reduce the sample size.

If you are on Python 2 (check with python --version), the syntax is mostly the same, but you should upgrade to Python 3 because Python 2 is no longer supported.

Frequently Asked Questions

Do I need to install anything to use random?

No. The random module is part of Python's standard library and is available in every standard installation.

How do I get 10 random numbers without duplicates?

Use random.sample(range(1, 101), 10). It returns 10 unique integers from the range you specify. Make sure the range contains at least 10 values.

How do I get random decimals instead of integers?

Use random.uniform(0, 1) inside a loop or list comprehension. That returns floats between 0 and 1. Adjust the arguments for a different range.

Why do I get the same numbers every time I run my script?

You probably called random.seed() with a fixed value. Remove that line, or change the seed value, to get different results each run.

Can I generate random numbers from a specific list?

Yes. Use random.choice(my_list) for one pick, or random.choices(my_list, k=10) for 10 picks with repetition allowed.

Is the random module suitable for passwords or security tokens?

No. Use the secrets module for anything security-related. The random module is designed for modeling and simulation, not cryptography.