The random.randrange()
method returns a randomly selected element from a given range. This method is similar to random.choice()
, except random.randrange()
does not build a range object.
random.randrange(start,stop,step)
start
(optional): An integer that specifies the position where the elements to be returned should start from.stop
(optional): An integer that specifies the position where the elements to be returned should end.step
(optional): An integer that specifies the increment in step size.Let’s use the random.randrange()
method to return elements for a given range.
import random# using the stop parameterprint(random.randrange(2))# using both the start and stop parametersprint(random.randrange(2,15))# using the start, stop and step parametersprint(random.randrange(2,15,3))
random.randrange()
method to return an integer, starting from 0 and ending at 2.random.randrange()
method to return an integer, starting from 2 and ending at 15.random.randrange()
method to return an integer, starting from 2 and ending at 15, and with a step size of 3.