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

Python randrange() function

Python randrange() function

Python Numbers Python 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

#!/usr/bin/python

import

random

Output 100 <= number < even numbers in the range of 1000

print

"randrange(100, 1000, 2) : "

,

random

.

randrange

(

100

,

1000

,

2

)

Output 100 <= number < other numbers in the range of 1000

print

"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 Python Numbers