How to generate a random value between two integers in Python

Use the randrange() method to generate a random integer in a given range.

The example below generates numbers in the range from 1 to 100.

Notice that the possible outcomes are from 1 to 99, 100 is excluded.

import random

print(random.randrange(1, 100))
20

If you want 100 as a possible outcome, use the randint() method.

import random

print(random.randint(1, 100))

print(random.randint(100, 100))
20
100

Watch on Youtube

You can also watch this content on Youtube: