Python3 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 (Python 3.0+)
#!/usr/bin/python3
#!/usr/bin/python3
import
random
Pick an odd number from 1-100
(
"
randrange(1,100, 2) :
"
,
random
.
randrange
(
1
,
100
,
2
)
)
Pick a random number from 0-99
(
"
randrange(100) :
"
,
random
.
randrange
(
100
)
)
Running the example above produces:
randrange(1,100, 2) : 97
randrange(100) : 42
Python3 Numbers