Python randrange() function
Description
randrange() The method returns a random number from a set with a specified increment, which defaults to 1.
Syntax
The syntax of the randrange() method is as follows:
import random
random.randrange ([start,] stop [,step])
**Note:** randrange() cannot be accessed directly; you need to import the random module and call the method through the static random object.
Parameters
- start -- the starting value of the range, included in the range.
- stop -- the ending value of the range, not included in the range.
- step -- the specified increment.
Return Value
Returns a random item from the given range.
Example
The following shows examples of using the randrange() method:
Example
#!/usr/bin/python
import
random
Output 100 <= number < even numbers in the range of 1000
"randrange(100, 1000, 2) : "
,
random
.
randrange
(
100
,
1000
,
2
)
Output 100 <= number < other numbers in the range of 1000
"randrange(100, 1000, 3) : "
,
random
.
randrange
(
100
,
1000
,
3
)
Running the example above produces:
randrange(100, 1000, 2) : 976
randrange(100, 1000, 3) : 520
Python Numbers