How to Get a Random Number in Python
What you’ll build or solve
You’ll generate random integers and floats in Python, pick a random item from a list, and set a seed for repeatable results.
When this approach works best
This approach works best when you need to:
Learn Python on Mimo
- Simulate chance in small programs, like dice rolls, coin flips, or simple games.
- Randomly sample or shuffle data for testing, like picking demo users or shuffling quiz questions.
- Add variety to output, like choosing a random message or suggestion.
Avoid this approach when:
- You need security, such as passwords, tokens, or verification codes. Use the
secretsmodule instead. - You need reproducible data across teams without managing seeds. Consider storing generated values instead.
Prerequisites
- Python 3 installed
- You know how to run Python code
- You know basic variables and lists
Step-by-step instructions
1) Import the random module
Most random number functions you want are in the random module.
importrandom
2) Get a random integer
Use randint(a, b) when you want both ends included.
importrandom
roll=random.randint(1,6)
print(roll)
This gives a number from 1 to 6, including both 1 and 6.
3) Get a random float
Use random.random() for a float between 0.0 and 1.0.
importrandom
x=random.random()
print(x)
If you want a float in a specific range, use uniform(a, b).
importrandom
temp=random.uniform(18.5,24.0)
print(temp)
4) Pick a random item from a list
Use choice() to select one element.
importrandom
colors= ["blue","green","red"]
pick=random.choice(colors)
print(pick)
If the list is empty, choice() raises an error. Always make sure the list has items.
5) Shuffle a list in place
Use shuffle() to reorder the list itself.
importrandom
cards= ["A","K","Q","J"]
random.shuffle(cards)
print(cards)
What to look for:
shuffle() returns None. The list changes in place.
6) Get repeatable results with a seed
Set a seed when you want the same “random” sequence each run, such as in tests or demos.
LUA
importrandom
random.seed(123)
print(random.randint(1,10))
print(random.randint(1,10))
print(random.randint(1,10))
With the same seed and the same sequence of random calls, you get the same results.
7) Use randrange() when you need an excluded end
Use randrange(start, stop) when you want stop excluded, which matches how range() works.
importrandom
n=random.randrange(1,7)# 1 to 6
print(n)
What to look for:
If you want numbers from 1 to 6, the stop must be 7 because it is not included.
Examples you can copy
1) Roll two dice and sum them
importrandom
die1=random.randint(1,6)
die2=random.randint(1,6)
total=die1+die2
print("Rolls:",die1,die2)
print("Total:",total)
2) Pick a random daily prompt
importrandom
prompts= [
"Write one small function.",
"Refactor one variable name.",
"Add one test case.",
"Fix one warning in your editor.",
]
print(random.choice(prompts))
3) Shuffle questions for a quiz
importrandom
questions= [
"What does len() return?",
"What is a list index?",
"What does import do?",
"What is a function?",
]
random.shuffle(questions)
forqinquestions:
print(q)
Common mistakes and how to fix them
Mistake 1: Forgetting to import random
You might write:
n=random.randint(1,10)
print(n)
Why it breaks:
Python does not know what random is, so you get a NameError.
Correct approach:
importrandom
n=random.randint(1,10)
print(n)
Mistake 2: Off-by-one ranges with randrange()
You might write:
importrandom
n=random.randrange(1,6)# expecting 1 to 6
print(n)
Why it breaks:
randrange(1, 6) stops before 6, so you never get 6.
Correct approach:
importrandom
n=random.randrange(1,7)# 1 to 6
print(n)
Mistake 3: Using random for security
You might write:
importrandom
code=random.randint(100000,999999)
print(code)
Why it breaks:
The random module is not designed for security-sensitive uses.
Correct approach:
importsecrets
code=secrets.randbelow(900000)+100000
print(code)
Troubleshooting
If you see NameError: name 'random' is not defined, add import random at the top of your file.
If your range never returns the top number, check if you used randrange() and forgot that the end is excluded.
If you get IndexError with choice(), your list is probably empty. Print len(your_list) before calling choice().
If results change between runs in a test, set a seed with random.seed(...) and keep the order of random calls the same.
If you are generating passwords or tokens, switch to secrets instead of random.
Quick recap
- Use
random.randint(a, b)for an integer where both ends are included. - Use
random.random()for0.0to1.0, orrandom.uniform(a, b)for a custom float range. - Use
random.choice(list)to pick one item, andrandom.shuffle(list)to reorder a list. - Use
random.seed(value)for repeatable results. - Use
random.randrange(start, stop)when you want an excluded end, and usesecretsfor security.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot