Python random.randrange() Method



The Python random.randrange() method returns a randomly selected element from the given range.

This method accepts two parameters which are start, stop. Therefore, it generates random numbers between the start value and the end value of the range.

Note − This function is not accessible directly, so we need to import random module and then we need to call this function using random static object.

Syntax

Following is the syntax of Python random.randrange() method −

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

Parameters

  • start − Start point of the range. This would be included in the range. By default it is 0.

  • stop − Stop point of the range. This would be excluded from the range.

  • step − Steps to be added in a number to decide a random number. By default it is 1.

Return Value

This method returns a random item from the given range

Example

The following example shows the usage of the Python random.randrange() method. Here the start value, the end value and the step size to be added in the number is passed as an argument to the randrange() method.

import random
# Select an even number in 100 <= number < 1000
print ("randrange(100, 1000, 2) : ", random.randrange(100, 1000, 2))
# Select another number in 100 <= number < 1000
print ("randrange(100, 1000, 3) : ", random.randrange(100, 1000, 3))

When we run above program, it produces following result −

randrange(100, 1000, 2) :  976
randrange(100, 1000, 3) :  520

Example

In here, only the start value and the end value is passed as an argument to the randrange() method. We are not providing the step size to be added in the number.:

import random
Start = 25
End = 45
randNo = random.randrange(Start,End)
print('The random number generated is:', randNo)

Following is an output of the above code −

The random number generated is: 32

Example

In the example given below we are creating the start value '25' and end value '45'. Then we are assigning a value to the total random numbers we need to generate in the list. Thereafter, a for loop is used to generate n random numbers. Inside this for loop we append each random number to the list. Then we invoke the randrange() method to generate random numbers.

import random
Start = 25
End = 45
TotalRandomNumbers = 5
List = []
for i in range(TotalRandomNumbers):
    randNo = random.randrange(Start, End)
    List.append(randNo)
print('The random numbers list is:', List)

While executing the above code we get the following output −

The random numbers list is: [36, 36, 42, 35, 26]

Example

If we pass a float value as an argument to this method, it raises a ValueError

import random
Start = 25.54
End = 45.67
randNo = random.randrange(Start,End)
print('The random number generated is:', randNo)

Output of the above code is as follows −

Warning (from warnings module):
  File "C:\Users\Lenovo\Desktop\untitled.py", line 4
    randNo = random.randrange(Start,End)
DeprecationWarning: randrange() will raise TypeError in the future
Traceback (most recent call last):
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\random.py", line 303, in randrange
    istart = _index(start)
TypeError: 'float' object cannot be interpreted as an integer
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\Lenovo\Desktop\untitled.py", line 4, in <module>
    randNo = random.randrange(Start,End)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\random.py", line 309, in randrange
    raise ValueError("non-integer arg 1 for randrange()")
ValueError: non-integer arg 1 for randrange()
python_numbers.htm
Advertisements