Python Generate Random Numbers Example
This beginner-friendly example shows how to generate random numbers in Python with the random module.
You will learn:
- How to import the
randommodule - How to generate one random integer
- How to generate several random numbers in a loop
- How
randint()differs from other random functions
If you want the shortest working example, start here:
import random
number = random.randint(1, 10)
print(number)
Use random.randint(start, end) to get a random integer between both numbers, including the endpoints.
What this example shows
This example focuses on the most common beginner use cases:
- Importing the
randommodule before using it - Generating a single random whole number
- Repeating that process several times with a loop
- Understanding when to use
randint(),random(),randrange(), anduniform()
If you are new to the module, see the Python random module overview.
Basic example: one random integer
Use import random first. Then call random.randint(1, 10).
import random
number = random.randint(1, 10)
print(number)
How it works
import randommakes the module availablerandom.randint(1, 10)returns a whole number- Both
1and10can be returned print(number)shows the result
This is a good first example because it is short and easy to test.
If you want a full explanation of this function, see random.randint() explained.
Generate multiple random numbers
You can use a for loop to generate several values.
import random
for i in range(5):
number = random.randint(1, 10)
print(number)
What this does
range(5)repeats the loop 5 times- Each loop generates a new random integer
- The output is usually different each time
If loops are still new to you, read Python for loops explained and Python range() function explained.
You can also store the values in a list if you want to use them later:
import random
numbers = []
for i in range(5):
numbers.append(random.randint(1, 10))
print(numbers)
This is useful for:
- Simple games
- Test values
- Sample data
- Practice projects
You can also write the same idea with a list comprehension:
import random
numbers = [random.randint(1, 10) for _ in range(5)]
print(numbers)
Other common random number options
randint() is not the only useful function in the random module.
random.random()
This returns a decimal number from 0 up to 1.
import random
number = random.random()
print(number)
Example output:
0.3478215941
random.randrange(start, stop)
This is similar to how range() works.
import random
number = random.randrange(1, 10)
print(number)
Important:
1can be returned10is not included
So randrange(1, 10) can return numbers from 1 to 9.
random.uniform(a, b)
This returns a random decimal number in a range.
import random
number = random.uniform(1, 10)
print(number)
Example output:
6.284193204
Expected output
Random output changes each time you run the program. That is normal.
For this code:
import random
print(random.randint(1, 10))
You might see:
3
Or:
8
Or:
10
Each run can produce a different result, and that is expected behavior.
Common beginner mistakes
Here are some common problems when generating random numbers in Python.
Forgetting to import random
This causes a NameError.
number = random.randint(1, 10)
print(number)
Fix it by adding the import:
import random
number = random.randint(1, 10)
print(number)
If you see this error, read NameError: name is not defined.
Using randint() without the module name
If you write import random, you must call the function with random. in front:
import random
number = random.randint(1, 10)
print(number)
This will not work:
import random
number = randint(1, 10)
print(number)
Confusing randint() and randrange()
These two functions are similar, but they do not handle the end value the same way.
random.randint(1, 10)returns1through10random.randrange(1, 10)returns1through9
Expecting the same result every time
Random functions usually produce different results on each run. That is the point of using them.
Naming your file random.py
If your script is named random.py, Python may try to import your file instead of the standard library module. This can cause strange errors such as an AttributeError.
Rename the file to something else, such as:
generate_numbers.pyrandom_example.pytest_random_values.py
Using float values with functions that expect integers
randint() and randrange() are meant for integer ranges.
This is not correct:
import random
number = random.randint(1.5, 10.5)
print(number)
Use integers with randint(), or use uniform() for decimal values.
When to use this
Generating random numbers is useful in many beginner projects, including:
- Picking a random number for a game
- Creating test values
- Choosing random positions or delays
- Building practice projects
A common next step is a guessing game. Try this related example: Python number guessing game example.
FAQ
How do I generate a random number between 1 and 10 in Python?
Use random.randint(1, 10). This includes both 1 and 10.
import random
print(random.randint(1, 10))
Why do I get NameError: name 'random' is not defined?
You need to add import random before using random.randint() or other random functions.
import random
print(random.randint(1, 10))
What is the difference between randint() and random()?
randint()gives a whole number in a rangerandom()gives a decimal number between0and1
Example:
import random
print(random.randint(1, 10))
print(random.random())
Can I generate several random numbers at once?
Yes. Use a loop or a list comprehension.
import random
numbers = [random.randint(1, 10) for _ in range(5)]
print(numbers)