Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 randrange() Function

Python3 randrange() Function

Python3 Numbers Python3 Numbers


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


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

print

(

"

randrange(1,100, 2) :

"

,

random

.

randrange

(

1

,

100

,

2

)

)

Pick a random number from 0-99

print

(

"

randrange(100) :

"

,

random

.

randrange

(

100

)

)

Running the example above produces:


randrange(1,100, 2) :  97
randrange(100) :  42

Python3 Numbers Python3 Numbers