Func Number Randrange
# Python randrange() Function
The `randrange()` method returns a randomly selected element from the range created by the specified start, stop, and step arguments. It is a highly efficient way to generate random integers within a specific sequence or interval.
---
## Description
The `randrange()` method selects a random number from a range of values generated by a specified increment (step). By default, the starting value is `0` and the step increment is `1`.
This method is part of Python's built-in `random` module. To use it, you must first import the `random` module and call the method using the `random` namespace.
---
## Syntax
The syntax for the `randrange()` method is as follows:
```python
import random
random.randrange([start,] stop [,step])
```
> **Note:** You cannot access `randrange()` directly. You must import the `random` module and call the method using `random.randrange()`.
---
## Parameters
* **`start`** *(Optional)*: The starting value of the range (inclusive). Defaults to `0` if omitted.
* **`stop`** *(Required)*: The ending value of the range (exclusive). The generated random number will always be strictly less than this value (`number < stop`).
* **`step`** *(Optional)*: The increment/spacing between numbers in the sequence. Defaults to `1` if omitted.
---
## Return Value
This method returns a randomly selected integer from the specified range.
---
## Code Examples
### Example 1: Basic Usage with Different Step Values
The following example demonstrates how to generate random numbers within a specific range using different step increments.
```python
#!/usr/bin/python3
import random
# Generate a random even number between 100 and 998 (100 <= number < 1000, step 2)
print("randrange(100, 1000, 2) : ", random.randrange(100, 1000, 2))
# Generate a random number from the sequence 100, 103, 106, ..., 997 (100 <= number < 1000, step 3)
print("randrange(100, 1000, 3) : ", random.randrange(100, 1000, 3))
```
**Possible Output:**
```text
randrange(100, 1000, 2) : 976
randrange(100, 1000, 3) : 520
```
---
### Example 2: Using Single Argument (Stop Only)
If you provide only a single argument, it is treated as the `stop` value. The range will start from `0` with a default step of `1`.
```python
import random
# Generate a random integer from 0 to 9 (0 <= number < 10)
num = random.randrange(10)
print("Random number from 0 to 9:", num)
```
---
## Considerations & Best Practices
### 1. `randrange()` vs `randint()`
While both functions generate random integers, they have a key difference in how they handle the upper bound:
* `random.randrange(start, stop)` is **exclusive** of the `stop` value (similar to Python's `range()` function).
* `random.randint(start, stop)` is **inclusive** of the `stop` value. It is equivalent to `random.randrange(start, stop + 1)`.
### 2. ValueError Exceptions
A `ValueError` will be raised in the following scenarios:
* If the `step` argument is set to `0`.
* If you pass non-integer arguments (e.g., floats) to the function.
* If the range is empty (e.g., `start` is greater than `stop` while `step` is positive).
YouTip