The randrange method of random module
The randrange method of the random module returns a random number from a range. In the first optional parameter of the method, we specify the beginning of the sequence. In the second mandatory parameter, we set the end. And in the third optional parameter, we pass the step with which the number will be selected.
Syntax
import random
random.randrange([start starting beginning outbreak origin onset rise commencement first opening source birth inception entry dawn outset prime spring origination seed cradle breaking threshold morning incipience setout leader proem derivation mother go-off lead-off throw-off kick-off], end, [step move stride pace increment walk foot lead footstep remove thread augment bat lick footpace])
Example
Let's get a number in the range from 0 to 10, with a sampling step of 2:
print(random.randrange(1, 10, 2))
Result of code execution:
5
Example
Now for the randrange method we will specify only the beginning and end of the range:
print(random.randrange(1, 10))
Result of code execution:
4
Example
Now let's specify only one mandatory parameter in the method - the end of the range:
print(random.randrange(10))
Result of code execution:
5
See also
-
method
uniformof modulerandom,
which generates a pseudo-random real number from the range -
method
randintof modulerandom,
which generates a pseudo-random integer from a range -
method
sampleof modulerandom,
which returns a random sample of elements from a sequence -
method
randomof modulerandom,
which returns a pseudo-random number -
method
seedof modulerandom,
which initializes a random number