The randrange
function in Python returns a randomly selected value from a specified sequence of numbers. It is a highly versatile function within Python's random
module, primarily used for generating random integers within a given range, similar to how the built-in range()
function creates a sequence.
Understanding randrange
The random.randrange()
function is a powerful tool for generating random integers. Unlike some other random number functions, randrange
allows for more control over the sequence from which the number is chosen, including specifying a step value. This enables the selection of random numbers with specific increments.
Syntax and Parameters
The randrange
function can be called with one, two, or three arguments:
random.randrange(stop)
: Returns a randomly selected integer from the range0
up to (but not including)stop
.- Example:
randrange(10)
could return any integer from0
to9
.
- Example:
random.randrange(start, stop)
: Returns a randomly selected integer fromstart
up to (but not including)stop
.- Example:
randrange(1, 11)
could return any integer from1
to10
.
- Example:
random.randrange(start, stop, step)
: Returns a randomly selected integer fromstart
up to (but not including)stop
, chosen from values incremented bystep
.- Example:
randrange(0, 10, 2)
could return0, 2, 4, 6, 8
.
- Example:
It's crucial to remember that the stop
value is always exclusive, meaning the returned number will never be equal to stop
.
Practical Examples of randrange
To use the randrange
function, you must first import the random
module.
-
Generating a random number from 0 to 9:
import random random_number = random.randrange(10) print(f"Random number (0-9): {random_number}")
-
Generating a random number between 1 and 10 (inclusive):
import random random_number = random.randrange(1, 11) # stop is 11 to include 10 print(f"Random number (1-10): {random_number}")
-
Generating a random even number between 0 and 100:
import random random_even = random.randrange(0, 101, 2) # step of 2 to ensure even numbers print(f"Random even number (0-100): {random_even}")
-
Simulating a dice roll (1-6):
import random dice_roll = random.randrange(1, 7) # Rolls a standard 6-sided die print(f"Dice roll: {dice_roll}")
Key Features and Use Cases
- Flexibility: The
step
argument makesrandrange
highly flexible, allowing selection from non-contiguous sequences (e.g., only even numbers, odd numbers, or multiples of a certain value). - Consistency with
range()
: Its behavior mirrors Python's built-inrange()
function, making it intuitive for those familiar with sequence generation. - Generating Indices: It's ideal for selecting a random index from a list or array because sequence indexing often starts from 0 and goes up to (but not including) the length.
- Game Development: Useful for simulating events like card draws, specific character movements, or target selection where numbers must fall within a defined set.
randrange
vs. randint
While both randrange
and randint
generate random integers, they differ in how they handle the upper bound of the range and their flexibility.
Feature | random.randrange(start, stop, step) |
random.randint(start, stop) |
---|---|---|
Range End | The stop value is exclusive (not included in the possible results). |
The stop value is inclusive (can be a possible result). |
Parameters | Accepts one (stop ), two (start , stop ), or three (start , stop , step ) arguments. |
Accepts only two arguments (start , stop ). |
Step | Allows specifying a step value to choose from specific increments. |
Does not support a step argument. |
Use Case | More flexible for sequences, especially when skipping values or aligning with range() . |
Simpler for basic inclusive integer ranges. |
For more detailed information, refer to the official Python documentation on the random
module: Python Standard Library — random
module.