Python: Generate a random integer between Min and Max

Last updated on November 10, 2022 A Goodman Loading... Post a comment

To generate a random integer between two given integers, you can use the randint method of the random module. The Min and Max are both included (that means one of them has a chance to be the result you get).

Example

import random

# Generate a random integer between 0 and 100
print(random.randint(0, 100))

# Generate a random integer between -100 and 100
print(random.randint(-100, 100))

# Generate a random integer between 200 and 500
print(random.randint(200, 500))

Output (the output will change each time you execute the code because of the randomness):

24
17
237

Hope this helps. Further reading:

You can also check out our Machine Learning category page or Python category page for more tutorials and examples.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related Articles