Python random randrange()

The random.randrange() function belongs to Python’s random module, and it returns random integers in a given range. It provides the flexibility to specify the start, stop, and step values, thereby making it a versatile function to generate random numbers in sequences.

Syntax

random.randrange(start, stop[, step])

Parameters

  1. start: The starting number of the range (default is 0 if omitted). This value is included in the range.
  2. stop: The range’s last number (this is necessary). The range does not include this value.
  3. step: The step size between numbers in the range (default is 1).

Returns

  • A randomly selected integer from the given sequence.

How It Works

  • It includes the range as [0, stop) when only stop is provided.
  • If start and stop are both given, the range is [start, stop).
  • If step is given, it defines the difference between consecutive numbers in the range.

Example 1: Basic Usage

import random

random_number = random.randrange(10)
print(f"Random number (0 to 9): {random_number}")

Explanation:

  • Here, only stop is specified.
  • The range will be [0, 1, 2, ..., 9].
  • From these values, one is chosen at random.

Possible Output:

Random number (0 to 9): 4

Example 2: Start and Stop

random_number = random.randrange(5, 15)
print(f"Random number (5 to 14): {random_number}")

Explanation:

  • Here, start = 5 and stop = 15.
  • The range will be [5, 6, 7, ..., 14].
  • The value 15 is excluded.

Possible Output:

Random number (5 to 14): 8

Example 3: Using Step

random_number = random.randrange(0, 21, 5)
print(f"Random number (multiple of 5 from 0 to 20): {random_number}")

Explanation:

  • Here, start = 0, stop = 21, and step = 5.
  • The range will be [0, 5, 10, 15, 20].
  • One is selected at random, and only these values are taken into account.

Possible Output:

Random number (multiple of 5 from 0 to 20): 10

Example 4: Negative Range

random_number = random.randrange(-10, 0)
print(f"Random number (-10 to -1): {random_number}")

Explanation:

  • Here, start = -10 and stop = 0.
  • The range will be [-10, -9, -8, ..., -1].

Possible Output:

Random number (-10 to -1): -3

Example 5: Random Odd Numbers

random_number = random.randrange(1, 20, 2)
print(f"Random odd number (1 to 19): {random_number}")

Explanation:

  • Here, start = 1, stop = 20, and step = 2.
  • The range will be [1, 3, 5, ..., 19].
  • The only numbers taken into account are odd ones.

Possible Output:

Random odd number (1 to 19): 15

Common Errors

Error 1: stop Less Than or Equal to start

random.randrange(10, 5)  # stop < start

Output:

ValueError: empty range for randrange()

Error 2: step is Zero

random.randrange(1, 10, 0)  # step = 0

Output:

ValueError: step argument must not be zero

Error 3: Non-Integer Arguments

random.randrange(1.5, 10)  # start is a float

Output:

TypeError: 'float' object cannot be interpreted as an integer

Comparison: random.randrange() vs random.randint()

random.randint()

  • Returns a random integer within the inclusive range [a, b].
  • Example:
random.randint(1, 10)  # Random integer from 1 to 10 (both inclusive)

random.randrange()

  • Returns a random integer within the half-open range [start, stop).
  • Example:
random.randrange(1, 11)  # Random integer from 1 to 10 (stop is excluded)

Practical Applications

  1. Random Indices: Generating random indices for selecting elements from a list.
items = ["apple", "banana", "cherry"]
index = random.randrange(len(items))
print(f"Random item: {items[index]}")

Output:

Random item: banana

2. Random Multiples: Generating random multiples of a number.

multiple = random.randrange(0, 101, 10)
print(f"Random multiple of 10 (0 to 100): {multiple}")

3. Simulation: Rolling dice with custom rules (e.g., only even rolls).

dice_roll = random.randrange(2, 13, 2)
print(f"Dice roll (even numbers only): {dice_roll}")

Conclusion

  • The random.randrange() function is powerful and can produce random numbers within a given range and step, efficiently.
  • Always remember the exclusive stop and proper handling of the step parameter to avoid errors.