Python randrange() function
Description
randrange()The method returns a random number from a set with a specified increment base, with the base defaulting to 1.
Syntax
The following is the syntax of the randrange() method:
import random random.randrange ([start,] stop [,step])
Note:randrange() is not directly accessible; you need to import the random module and then call the method through the random static object.
Parameter
- start -- The starting value within the specified range, inclusive.
- stop -- The ending value within the specified range, exclusive.
- step -- The specified increment base.
Return Value
Return a random item from the given range.Example
The following shows an example of using the randrange() method:
Example
#!/usr/bin/python
import random
# Output even numbers in the range 100 <= number < 1000
print "randrange(100, 1000, 2) : ", random.randrange(100, 1000, 2)
# Output other numbers in the range 100 <= number < 1000
print "randrange(100, 1000, 3) : ", random.randrange(100, 1000, 3)
import random
# Output even numbers in the range 100 <= number < 1000
print "randrange(100, 1000, 2) : ", random.randrange(100, 1000, 2)
# Output other numbers in the range 100 <= number < 1000
print "randrange(100, 1000, 3) : ", random.randrange(100, 1000, 3)
The output result of the above example after running is:
randrange(100, 1000, 2) : 976 randrange(100, 1000, 3) : 520other extensions