Last modified: Mar 30, 2026 By Alexander Williams
Generate Random Number in Range Python
Generating random numbers is a common task in programming.
You might need them for simulations, games, or testing.
Python makes this easy with its built-in random module.
This guide shows you how to get random numbers within a specific range.
Import the Random Module
First, you need to import the module.
Use the import statement at the top of your script.
import random
This gives you access to all the random number functions.
Using random.randint() for Integers
The random.randint() function is the most straightforward.
It returns a random integer between two numbers, inclusive.
This means both the start and stop values are possible results.
Understanding inclusivity is key, similar to concepts in the Python Range Inclusive? Stop Value Explained article.
The syntax is simple: random.randint(a, b).
Parameter a is the lower bound, and b is the upper bound.
# Generate a random integer between 1 and 10 (including 1 and 10)
random_integer = random.randint(1, 10)
print(f"Random integer between 1 and 10: {random_integer}")
Random integer between 1 and 10: 7
Each time you run this code, you'll get a different number like 3, 9, or 1.
Using random.randrange() for More Control
The random.randrange() function offers more flexibility.
It works similarly to the standard Python Range Function.
You can specify a start, stop, and step value.
Important: The stop value is exclusive, just like the standard range().
Its syntax can be random.randrange(stop) or random.randrange(start, stop, step).
# Generate a random integer from 0 up to (but not including) 10
num1 = random.randrange(10)
print(f"Number from 0 to 9: {num1}")
# Generate a random integer between 5 and 20
num2 = random.randrange(5, 21)
print(f"Number from 5 to 20: {num2}")
# Generate a random odd number between 1 and 50
num3 = random.randrange(1, 51, 2)
print(f"Random odd number from 1 to 50: {num3}")
Number from 0 to 9: 4
Number from 5 to 20: 14
Random odd number from 1 to 50: 37
Using random.uniform() for Floating-Point Numbers
Need a random decimal number? Use random.uniform().
It returns a random float between the two given numbers.
The result can be any value in that continuous range.
This is useful when you need precision beyond whole numbers.
For generating sequences of floats, you can explore methods in the Python Range Float guide.
# Generate a random float between 0.0 and 1.0
random_float = random.uniform(0.0, 1.0)
print(f"Random float between 0.0 and 1.0: {random_float:.4f}") # Formatted to 4 decimals
# Generate a random float between -5.5 and 10.5
random_float2 = random.uniform(-5.5, 10.5)
print(f"Random float between -5.5 and 10.5: {random_float2:.2f}")
Random float between 0.0 and 1.0: 0.7231
Random float between -5.5 and 10.5: 3.87
Practical Examples and Common Use Cases
Let's see how these functions work in real scenarios.
Example 1: Simulating a Dice Roll
# Simulate rolling a standard six-sided die
dice_roll = random.randint(1, 6)
print(f"You rolled a: {dice_roll}")
You rolled a: 4
Example 2: Picking a Random List Element
You can combine randint() or randrange()
colors = ['Red', 'Blue', 'Green', 'Yellow', 'Purple']
# Get a random index for the list
random_index = random.randrange(len(colors))
chosen_color = colors[random_index]
print(f"Randomly chosen color: {chosen_color}")
Randomly chosen color: Green
Example 3: Generating Random Test Data
# Generate 5 random temperatures between 18.0 and 25.0 degrees
print("Simulated temperatures:")
for i in range(5):
temp = random.uniform(18.0, 25.0)
print(f" Sample {i+1}: {temp:.1f}°C")
Simulated temperatures:
Sample 1: 21.3°C
Sample 2: 19.8°C
Sample 3: 24.1°C
Sample 4: 22.5°C
Sample 5: 18.7°C
Key Differences and When to Use Each
Choosing the right function depends on your needs.
Use random.randint(a, b) when you need an integer and both bounds are inclusive. It's simple and perfect for dice, cards, or page numbers.
Use random.randrange(start, stop, step) when you need the extra control of an exclusive stop or a step value. It mirrors the behavior of the range() function you use in loops.
Use random.uniform(a, b) when you need a decimal number. This is ideal for scientific data, random measurements, or any continuous value.
Conclusion
Generating random numbers in a range is simple in Python.
Remember random.randint() for inclusive integer ranges.
Use random.randrange() for exclusive stops and steps.
Choose random.uniform() for floating-point numbers.
The random
Start experimenting with these functions in your own projects today.