Last modified: Mar 28, 2026 By Alexander Williams
Python Random Number in Range: A Complete Guide
Generating random numbers is a common task in programming. You might need them for games, simulations, or testing.
In Python, the random module makes this easy. This guide focuses on getting random numbers within a specific range.
We will explore the key functions. You will learn how to use them with clear examples.
Importing the Random Module
First, you need to import the module. This gives you access to all its functions.
# Import the random module
import random
Now you are ready to generate random numbers. Let's look at the main functions for ranges.
Using randint for Inclusive Ranges
The randint function is the most straightforward. It returns a random integer.
The key point is that both the start and stop values are included. This is different from the standard range function, which excludes the stop value. If you need a refresher on how range works, see our Python Range Function Guide.
Its syntax is simple: random.randint(a, b). The number can be a, b, or anything in between.
import random
# Generate a random integer between 1 and 10 (inclusive)
my_number = random.randint(1, 10)
print(f"Random integer between 1 and 10: {my_number}")
Random integer between 1 and 10: 7
Run this code multiple times. You will get different results like 3, 10, or 5. The value 10 is possible because the range is inclusive.
Using randrange for Flexible Integer Ranges
The randrange function is more flexible. It mimics the behavior of the range function.
You can specify a start, stop, and step. Importantly, the stop value is excluded, just like in a standard range. For more on including the stop value, check our guide on Python Range Inclusive.
Its syntax is random.randrange(start, stop, step). Only `stop` is required.
import random
# Get a number from 0 up to (but not including) 10
num1 = random.randrange(10)
print(f"Number from 0 to 9: {num1}")
# Get a number from 5 up to (but not including) 15
num2 = random.randrange(5, 15)
print(f"Number from 5 to 14: {num2}")
# Get an even number between 0 and 20
num3 = random.randrange(0, 21, 2)
print(f"Even number between 0 and 20: {num3}")
Number from 0 to 9: 4
Number from 5 to 14: 11
Even number between 0 and 20: 16
Use randrange when you need the classic Python range behavior with random selection.
Using uniform for Floating-Point Numbers
Need a random decimal number? Use the uniform function.
It returns a random float between two numbers. The result can be any value in the interval, including the endpoints.
The syntax is random.uniform(a, b). Whether a or b is larger does not matter.
import random
# Get a random float between 2.5 and 7.5
float_num = random.uniform(2.5, 7.5)
print(f"Random float between 2.5 and 7.5: {float_num:.2f}") # Formatted to 2 decimals
# The order of arguments doesn't matter
float_num2 = random.uniform(10, 5)
print(f"Random float between 10 and 5: {float_num2:.2f}")
Random float between 2.5 and 7.5: 4.73
Random float between 10 and 5: 7.18
This function is perfect for scientific calculations or any task requiring decimal precision.
Practical Examples and Common Use Cases
Let's see how these functions work in real scenarios.
Example 1: A Simple Dice Roll. A standard die has faces from 1 to 6. We use randint because both 1 and 6 are valid rolls.
import random
def roll_dice():
return random.randint(1, 6)
print("Rolling the dice...")
for i in range(3):
print(f"Roll {i+1}: {roll_dice()}")
Rolling the dice...
Roll 1: 3
Roll 2: 6
Roll 3: 1
Example 2: Selecting a Random List Item. You can generate a random index.
import random
colors = ['Red', 'Blue', 'Green', 'Yellow', 'Purple']
# Generate a valid index from 0 to 4 (length-1)
random_index = random.randrange(len(colors))
selected_color = colors[random_index]
print(f"Available colors: {colors}")
print(f"Randomly selected color: {selected_color}")
Available colors: ['Red', 'Blue', 'Green', 'Yellow', 'Purple']
Randomly selected color: Green
Important Considerations
Remember these points when working with random numbers.
Reproducibility with a Seed: For testing, you might need the same "random" sequence every time. Use random.seed().
import random
random.seed(42) # Set the seed to a fixed value
print(random.randint(1, 100))
print(random.randint(1, 100))
random.seed(42) # Reset to the same seed
print(random.randint(1, 100)) # Will print the same first number again
82
15
82
Security Warning: The random module is not cryptographically secure. Do not use it for passwords or encryption. Use the secrets module for that.
Conclusion
Generating a random number in a range is simple in Python. You have three main tools.
Use randint for integers where both ends are included. Use randrange for integer ranges that mimic the range function's exclusive stop. Use uniform for floating-point numbers.
Choose the function that matches your data type and range logic. With these functions, you can easily add randomness to your Python programs for games, simulations, and more.