.randint()

The .randint() method returns a random integer that exists between two int values, int_a and int_b (inclusive), passed as arguments.

Syntax

random.randint(int_a, int_b)

Another way of writing this would be random.randrange(int_a, int_b + 1).

Example

In the example below, .randint() is used to return a random number between 0 and 50:

import random
print(random.randint(0, 50))

Codebtye Example

The .randint() method can also be applied to negative int values, as shown in the example below:

Code
Output
Loading...

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Python on Codecademy