Easy Tutorial
❮ Python Os Open Python Class ❯

Python3 randrange() Function

Python3 Numbers


Description

The randrange() method returns a random number from the specified incremental base set, with the default base value being 1.


Syntax

Here is the syntax for the randrange() method:

import random

random.randrange([start,] stop [,step])

Note: randrange() cannot be accessed directly. It requires importing the random module and then calling this method via the random static object.


Parameters


Return Value


Example

Below are examples demonstrating the use of the randrange() method:

Example (Python 3.0+)

#!/usr/bin/python3
import random

# Select an odd number between 1 and 100
print("randrange(1, 100, 2) : ", random.randrange(1, 100, 2))

# Select a random number between 0 and 99
print("randrange(100) : ", random.randrange(100))

The output of the above examples is:

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

Python3 Numbers

❮ Python Os Open Python Class ❯