Python: Generate Multiple Random Numbers (Guide)
In this article
To generate multiple random numbers in Python, use the random module from the standard library. The shortest working method is random.sample(range(1, 101), 5) for unique numbers or [random.randint(1, 100) for _ in range(5)] for numbers that may repeat. No installation is required because random ships with Python. The steps below cover both approaches, how to control repetition, how to set a seed for reproducible results, and what to do when the output looks wrong.
What You'll Need
- A working Python installation (any recent version 3.x). You can check by running
python --versionorpython3 --versionin a terminal. - A way to run Python code: the interactive interpreter, a saved
.pyfile, or an editor such as IDLE, VS Code, or Thonny. - No third-party packages. The
randommodule is part of the standard library.
Step-by-Step
Import the module. At the top of your script or in the interpreter, type
import random. This gives you access to all the functions below. If you only need one or two functions, you can writefrom random import randint, sample, but the plain import is clearer for beginners.Decide whether repeats are allowed. This is the most important choice. If you are simulating dice rolls or picking lottery-style numbers where duplicates are acceptable, use a loop with
randint. If you are drawing cards, picking raffle winners, or sampling survey respondents, you need unique values, so usesampleorchoiceswith care. Mixing these up is the most common source of confusing output.Generate numbers that may repeat. Use a list comprehension:
numbers = [random.randint(1, 100) for _ in range(5)]. This produces five integers between 1 and 100 inclusive, and the same number can appear more than once. Change5to however many you need and adjust the bounds. For floats between 0 and 1, use[random.random() for _ in range(5)]; for floats in a range, userandom.uniform(1.0, 10.0)inside the loop.Generate unique numbers. Use
random.sample(range(1, 101), 5). The first argument is the population to draw from, and the second is how many unique items you want. The upper bound inrangeis exclusive, sorange(1, 101)includes 100. If you ask for more items than the population contains, Python raises aValueError, so check that your sample size is not larger than the range.Pick from a list of values. If your numbers are not a simple range, pass a list directly:
random.sample([10, 20, 30, 40, 50], 3). This returns three distinct items from that list. For weighted selection where some values are more likely, userandom.choices(population, weights=..., k=n), which allows repeats by default.Print or store the result. Assign the output to a variable and print it:
print(numbers). If you need the numbers one per line, loop over the list withfor n in numbers: print(n). To reuse them later, save them to a file withopen('numbers.txt', 'w')and write each value, or keep them in memory for the rest of your program.Make results reproducible (optional). Call
random.seed(42)before generating numbers. The same seed produces the same sequence every run, which is useful for testing and debugging. Do not use a fixed seed in production if you need unpredictable output; omit the seed or seed from a source such asrandom.seed()with no argument, which uses system entropy.Run and verify. Execute your script with
python yourfile.pyor run the lines in the interpreter. Check that the count matches what you asked for, that values fall inside your bounds, and that uniqueness is what you intended. If any of these is off, revisit step 2 and the function you chose.
Common Mistakes
- Using
randintwhen you need unique values.randintcan repeat, so a list of five "winners" may contain duplicates. Usesampleinstead. - Off-by-one with
randrange.random.randrange(1, 10)stops at 9, not 10, because the upper bound is exclusive. Userandint(1, 10)if you want 10 included. - Calling the function once. Writing
random.randint(1, 100)gives a single number, not a list. Wrap it in a loop or comprehension to get multiple values. - Seeding inside the loop. Calling
random.seed()repeatedly resets the generator and can produce the same number each time. Seed once at the start. - Assuming
randomis cryptographically secure. It is not. For passwords, tokens, or security-sensitive values, use thesecretsmodule instead.
Tips
- Use
random.choices(population, k=n)when you want n picks with replacement in one call; it is faster and clearer than a manual loop. - For reproducible tests, set the seed at the top of your test file and document it so others can rerun the same sequence.
- If you need random numbers for simulations, consider
numpy.randomfor large arrays, but the standardrandommodule is fine for everyday scripts. - Keep bounds as variables (for example
low = 1,high = 100) so you can change them in one place. - Remember that
samplereturns a new list and does not modify the original population.
Troubleshooting
You get a ValueError: Sample larger than population. Your sample size exceeds the number of available values. Either increase the range or reduce the count. For example, random.sample(range(1, 6), 5) works because the range holds five values, but asking for six fails.
You get the same numbers every run. Check whether you called random.seed() with a fixed value. Remove it or change the seed if you want different output. Also confirm you are not re-seeding inside a loop.
You get a NameError: name 'random' is not defined. You forgot import random at the top of the file. Add it and rerun.
Numbers fall outside the expected range. Review whether you used randrange (exclusive upper bound) or randint (inclusive). Also check that you passed the bounds in the right order; randint(100, 1) raises an error because the low value is greater than the high value.
The output is a single number instead of a list. You called the function once. Wrap it in a list comprehension or use choices/sample to get multiple values in one step.
You need unpredictable values for security. Switch to secrets.randbelow() or secrets.choice(). The random module is designed for modeling and simulation, not cryptography.
Frequently Asked Questions
How do I generate 10 random numbers between 1 and 100 in Python?
Use [random.randint(1, 100) for _ in range(10)] for numbers that may repeat, or random.sample(range(1, 101), 10) for unique numbers.
What is the difference between random.sample and random.choices?
sample returns unique items without replacement; choices can return duplicates and supports weights.
Can I generate random floats instead of integers?
Yes. Use random.random() for floats between 0 and 1, or random.uniform(a, b) for floats in a custom range.
Why do I get the same random numbers every time I run my script?
You likely set a fixed seed with random.seed(value). Remove the seed or use a variable seed to get different results each run.
Is the random module suitable for passwords?
No. Use the secrets module for passwords, tokens, and any security-related randomness.
How do I generate random numbers without importing anything?
You cannot; the random module is part of the standard library and must be imported. There is no built-in function for random numbers outside a module.