YouTip LogoYouTip

Ref Random Randrange

## Python random.randrange() Method The `random.randrange()` method is a built-in function in Python's `random` module. It returns a randomly selected element from a specified range of integers. This method is highly efficient and behaves similarly to Python's built-in `range()` function, but instead of returning a sequence, it returns a single random integer from that sequence. --- ### Syntax ```python random.randrange(start, stop, step) ``` #### Parameter Values | Parameter | Type | Required/Optional | Description | | :--- | :--- | :--- | :--- | | **`start`** | Integer | Optional | Specifies the starting value of the range. Defaults to `0` if omitted. | | **`stop`** | Integer | Required | Specifies the exclusive upper limit of the range. The returned value will always be less than this number. | | **`step`** | Integer | Optional | Specifies the incrementation between numbers in the range. Defaults to `1` if omitted. | #### Return Value * Returns a single **integer** chosen randomly from the specified range. --- ### Code Examples #### Example 1: Basic Usage (Generate a number between 1 and 8) The following example returns a random integer from `1` up to (but not including) `9` (i.e., $1 \le x < 9$). ```python import random # Generate a random number between 1 and 8 result = random.randrange(1, 9) print(result) ``` **Possible Output:** ```text 8 ``` #### Example 2: Using Only the `stop` Parameter If you provide only one argument, it is treated as the `stop` value. The range will start from `0` with a step of `1`. ```python import random # Generate a random number from 0 to 9 (10 is excluded) result = random.randrange(10) print(result) ``` **Possible Output:** ```text 4 ``` #### Example 3: Using the `step` Parameter You can use the `step` parameter to generate random numbers that follow a specific pattern, such as even numbers, odd numbers, or multiples of five. ```python import random # Generate a random even number between 0 and 100 (inclusive of 0, exclusive of 100) even_num = random.randrange(0, 100, 2) print(f"Random even number: {even_num}") # Generate a random multiple of 5 between 5 and 50 (exclusive of 50) multiple_of_five = random.randrange(5, 50, 5) print(f"Random multiple of 5: {multiple_of_five}") ``` **Possible Output:** ```text Random even number: 42 Random multiple of 5: 35 ``` --- ### Considerations & Best Practices 1. **Exclusive Upper Bound (`stop`):** The `stop` value is **never** included in the possible outcomes. For example, `random.randrange(1, 5)` can only return `1`, `2`, `3`, or `4`. 2. **`random.randrange()` vs `random.randint()`:** * `random.randrange(start, stop)` does **not** include the `stop` value (exclusive). * `random.randint(start, stop)` **includes** the `stop` value (inclusive). Under the hood, `random.randint(a, b)` is equivalent to `random.randrange(a, b + 1)`. * `random.randrange()` supports a `step` argument, whereas `random.randint()` does not. 3. **ValueError:** If the range is empty (for example, if `start` is greater than `stop` and the `step` is positive, e.g., `random.randrange(10, 1)`), Python will raise a `ValueError: empty range for randrange()`.
← Jsref Isinteger NumberPython Random β†’