Usage
The randrange()
method returns an integer number randomly selected from the specified range.
Syntax
random.randrange(start, stop, step)
Parameters
Parameter | Condition | Description |
start | Optional | An integer that specifies where to start the range. Default value is 0. |
stop | Required | An integer that specifies where to end the range. This value is exclusive. |
step | Optional | An integer that specifies the increment between each number in the range. Default value is 1. |
It is important to note that the stop
parameter is exclusive, which means that randrange()
does not consider the stop
number when generating a random integer.
Module Import
To use the randrange()
method, you need to import the random module first.
import random
How to use the randrange() Function?
There are three ways to use the randrange()
function:
randrange(stop)
: With only thestop
argument provided, you will get a random integer between 0 (inclusive) and the number you specify as thestop
(exclusive).randrange(start, stop)
: When you provide bothstart
andstop
arguments, you define both the lower bound (start
) and upper bound (stop
) of the desired range of random numbers.randrange(start, stop, step)
: By including thestep
argument, you determine the increment between possible random numbers within the range.
Let’s break down each use case with examples.
randrange(stop)
When you call randrange()
with just one argument, you will get a random integer between 0 (inclusive) and the number you specify as the stop
(exclusive).
import random
# Generate a random number between 0 and 10 (exclusive)
print(random.randrange(10))
# Possible output: 6
In this example, only the stop
parameter is set to 10. This means the method will generate a random integer starting from the default start
value, which is 0, up to but not including 10. So the possible values that can be generated are: 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9.
Below are some more examples:
print(random.randrange(100))
# Possible output: 53
print(random.randrange(50))
# Possible output: 49
randrange(start, stop)
When you call randrange()
with two arguments, you get to decide not only the upper bound but also the lower bound of the range, so your range does not always start at 0.
Let’s see how to generate a random integer between 5 and 10 (exclusive):
print(random.randrange(5, 10))
# Possible output: 6
In this example, both the start
and stop
parameters are provided. The method will produce a random integer between the value of start
(which is 5 in this case) up to but not including the stop
value (which is 10). So the possible values that can be generated are: 5, 6, 7, 8, or 9.
Below are some more examples:
print(random.randrange(60, 100))
# Possible output: 73
print(random.randrange(150, 1000))
# Possible output: 428
You can generate a negative random number as well.
print(random.randrange(-10, 10))
# Possible output: -2
randrange(start, stop, step)
When you call randrange()
with three arguments, you not only specify the lower and upper bounds of the range but also the interval between potential numbers. If you do not specify a step
value, randrange()
will assume that the step
is 1.
print(random.randrange(5, 20, 5))
# Possible output: 15
In this example, all three parameters start
, stop
, and step
are provided. The method will generate a random integer from the range starting from 5 (inclusive) to 20 (exclusive), but only in increments defined by the step
value of 5. So, the method considers values 5, 10, and 15 as potential outputs.
Below are some more examples:
print(random.randrange(3, 30, 3))
# Possible output: 24
print(random.randrange(7, 70, 7))
# Possible output: 7
When the step
is negative, the range of possible values is generated in descending order, starting from the start
value and moving towards the stop
value, but not including the stop
value itself.
print(random.randrange(50, 10, -5))
# Possible output: 15
print(random.randrange(50, -50, -10))
# Possible output: -40
Note that for a negative step
, it’s common (and necessary) for the start
value to be greater than the stop
value.
If the range defined by the start
, stop
, and step
arguments is empty, randrange()
will raise a ValueError
.
print(random.randrange(-10))
# Triggers ValueError: empty range for randrange()
print(random.randrange(50, 10))
# Triggers ValueError: empty range for randrange()
print(random.randrange(10, 50, -5))
# Triggers ValueError: empty range for randrange()
randrange() vs randint()
While both randrange()
and randint()
are useful for generating random integers, they have subtle differences in how they work.
randint(start, stop)
returns a random integer within a completely inclusive range. This means both thestart
andstop
values you provide can be included in the possible results.- On the other hand,
randrange([start], stop[, step])
offers more flexibility. It generates a random integer within a range where thestop
value is exclusive (meaning it won’t be included as a possible result). Additionally, the optionalstep
argument allows you to specify the increments between potential outcomes.
In a nutshell, randint(start, stop)
is equivalent to calling randrange(start, stop + 1)
.
So, randint() is straightforward and more suitable when you need a random integer within a specified range, including both endpoints. However, if you require more fine-grained control over the range, especially with regular intervals other than 1, randrange()
is the preferable choice.