Skip to content

Python Random randrange() Function Vijetha Spark By {Examples}

  • by

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 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() 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 >= stop, ValueError will be returned.

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

You can use a random.randrange() function of a random module is one the most useful function for generating random numbers from specified range and interval. Let’s use randrange() and 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.

4. Get Python Random Number Between 1 to 100 Range

You can also get the random number between the range of 1 to 100 using the randrange() function. Let’s use randrange() and get the random integer between 1 to 100(exclude) 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.

5. Generate a random negative integer from Range in Python

Alternatively, we can get the negative random number by providing 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 notice how it generates the 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

When we pass a non-integral number into the randrange() function, it will raise 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!!

 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 signatures random.randrange(stop)  Read More Python, Python Tutorial 

Leave a Reply

Your email address will not be published. Required fields are marked *