How to Generate a Random Number in Python
· 1 min read · Updated March 16, 2026 · beginner
python random numbers
Python’s random module is the standard way to generate random numbers for games, simulations, and testing.
Random Float Between 0 and 1
The simplest approach gives you a float in the range [0.0, 1.0):
import random
number = random.random()
print(number)
# 0.8471536681046147
Random Integer in Range
Use randint(a, b) for inclusive bounds:
import random
# Dice roll (1-6)
die = random.randint(1, 6)
print(die)
# 4
# Random number between 1 and 100
num = random.randint(1, 100)
print(num)
# 73
Random Float in Range
Use uniform(a, b) for floats between two values:
import random
price = random.uniform(10.0, 50.0)
print(f"${price:.2f}")
# $23.41
Random Element from a List
Pick a random item:
import random
colors = ["red", "green", "blue"]
choice = random.choice(colors)
print(choice)
# green
Setting a Seed for Reproducibility
If you need the same sequence each run:
import random
random.seed(42)
print(random.randint(1, 100))
# 46
# Reset to get same result
random.seed(42)
print(random.randint(1, 100))
# 46