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

Python random randint() Method

Python random randint() Method

Python random Module Python random Module

Python random.randint() The method returns an integer within a specified range.

randint(start, stop) is equivalent to randrange(start, stop+1)。

Syntax

The syntax of the random.randint() method is as follows:

random.randint(start, stop)

Parameter Description:

Return Value

Returns an integer within the specified range.

Example

The following example returns a number between 1 and 10 (greater than or equal to 1, less than or equal to 10):

Example

Import the random package

import

random

Generate a random integer between 1 and 10

random_number

=

random

.

randint

(

1

,

10

)

print

(

random_number

)

Output result:


8

In the above example, random.randint(1, 10) generates a random integer between 1 and 10 (including 1 and 10) and assigns it to the random_number variable.

Every time this code runs, a different random integer is produced.

Notes:

Python random Module Python random Module