Python randrange () function
description
randrange () method returns the specified increments cardinality of a random number, the base defaults to 1.
grammar
The following is the syntax 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 by random static objects.
parameter
- start - start value within a specified range, it included in the scope.
- stop - end value within a specified range, not included in the scope.
- step - increment specified base.
return value
Returns a random item from the given range.Examples
The following shows an example of using randrange () method:
#!/usr/bin/python import random # 输出 100 <= number < 1000 间的偶数 print "randrange(100, 1000, 2) : ", random.randrange(100, 1000, 2) # 输出 100 <= number < 1000 间的其他数 print "randrange(100, 1000, 3) : ", random.randrange(100, 1000, 3)
After running the above example output is:
randrange(100, 1000, 2) : 976 randrange(100, 1000, 3) : 520