In Python, the random module allows you to generate random integers. It’s often used when you need to a number to be picked at random or to pick a random element from a list. Using it is actually really straightforward. Let’s say you want to print a random integer within a given range, like 1-100. Here’s how you would write that code:
import random print random.randint(1, 100)
- Python Programming – The Random Module
- How to Generate a Random Number in Python | Python Program to Generate Random Numbers
- Python Programming – Numbers
The code above will return a random number from 0 to 100. To write the code, you just pass the range of numbers you want the random integer to be pulled from through the parentheses parameters. So if you want the range to be 1-100, the first parameter is 1, and the last parameter is 100 — pretty simple!