Python Random randrange() Function

Spread the love

Python provides the randrange() function from the random module to generate a random number between the given range of integral numbers along with the step value. It takes three parameters start, stop, and step, and it returns random numbers between the start and stop(excluding) along with a specified interval.

You can use this method with variations of signatures random.randrange(stop) and random.randrange(start, stop[, step]) that generate random integers from a range.

The first form random.randrange(stop) returns a random integer from the range [0, stop), meaning that the result will be greater than or equal to 0, but less than stop.

In this article, I will explain Python random.randrange() function syntax, parameters, and usage of how we can generate the random integer within a given range by specifying step value.

1. Quick Examples of randrange() Function

If you are in a hurry, below are some quick examples of the randrange() function.


#  Quick examples of randrange() function.

# Example 1: Generate random number between 1 to 10
# using randrange()
random_value= random.randrange(1, 10, 2)

# Example 2: Generate random integer from 1 to 100 
# using randrange()
random_value= random.randrange(1, 100, 10)

# Example 3: Generate random integer of negative using randrange()
random_value= random.randrange(-20, -10)

# Example 4: Generate positive/negative random 
# integer using randrange()
for i in range(3):
    random_value= random.randrange(-20, 10)
    print(random_value)

2. Python random.randrange() Function

The randrange(start,stop,step) from a random module is used to generate the random number between the given range with specified step. It takes start and end numeric values as its parameters, so that a random number is generated within this specified range.

  • Note: If we specify parameters with non integral values or start >= stopValueError will be returned.

random.randrange(stop) returns a random integer from the range [0, stop), meaning that the result will be greater than or equal to 0, but less than stop.

2.1 Syntax of randrange() Function

Following is the syntax of the randrange() Function.


# Syntax of randrange() function
random.randrange(start, stop[, step])  

2.2 Parameter of randrange()

  • start:  (Optional) It specifies starting point of the range. The default value is 0.
  • stop: (Required) It specifies the ending point of the range. It does not include as a part of generating a random number.
  • step: (optional) It specifies the increment value of given a range of numbers. It takes ‘1’ as the increment value if it is not specified.

2.3 Return Value

It returns randomly generated numbers between the given range along with specified increments.

3. Get Random Number Between 1 to 10 Range in Python

Let’s use the Python random.randrange() function to generate random numbers from specified range of intervals. In the below example, we get the random integer between 1 to 10(exclude) with step value ‘2’.

Related: Generate Random Integers Between 0 and 9 in Python


import random

# Generate random number between 1 to 10
# using randrange()
random_value= random.randrange(1, 10, 2)
print("Random number between 1 to 10:", random_value)

Yields below output.

Python random range

4. Get Python Random Number Between 1 to 100 Range

Let’s see another example of getting the random number between the range of 1 to 100 with step value 10.


import random

# Generate random integer from 1 to 100 
# using randrange()
random_value= random.randrange(1, 100, 10)
print("Random number between 1 to 100:", random_value)

Yields below output.

Python random range

5. Generate a random negative integer from Range in Python

Alternatively, we can get the negative random number by providing a range of negative numbers into randrange() function.


import random

# Generate random integer of negative using randrange()
random_value= random.randrange(-20, -10)
print("Random integer:", random_value)

# Output:
# Random integer: -15

We can also get the random number of positive/negative using the randrange() function. Providing a range of numbers with positive(stop) and negative (start) into the randrange() function will generate either a positive random number or a negative random number.

Let’s run the code multiple times and to generate multiple random numbers.


import random

# Generate positive/negative random 
# integer using randrange()
print("Random Integer:")
for i in range(3):
    random_value= random.randrange(-20, 10)
    print(random_value)

# Output:
# Random Integer:
# -12
# -15
#  9

6. Generate Random Float Number

randrange() doens’t support floating values, by using the floats it raises the ValueError.


import random

# Generate random integer using randrange()
random_value= random.randrange(15, 20.2)
print("Random number of float:", random_value)

# Output: ValueError: non-integer stop for randrange()

7. Conclusion

In this article, I have explained Python random.randrange() function syntax, parameters, and usage of how to generate the random integer within a given range by specifying step value. And also explained when we pass non-integral numbers into this function it generates a ValueError.

Happy Learning!!

References

Vijetha

With 5 of experience in technical writing, I have had the privilege to work with a diverse range of technologies like Python, Pandas, NumPy and R. During this time, I have consistently demonstrated my ability to grasp intricate technical details and transform them into comprehensible materials.

Leave a Reply