Here is how to generate a random integer in Python.


# The library required to generate random numbers
import random 
# Generate a random int between 1 and 10
random_integer = random.randint(1, 10) 
# Print out the generated integer
print(random_integer) 

Output

4


Explanation

The Python randint() can generate random integers by getting as arguments the lower and the upper bound integers of the range you want the integer to be picked from.


Related HowTos
Deploy a web app to Heroku
Schedule a Python script for execution at a specific time every day
Store Python passwords securely on Windows, Mac, and Linux
Do dictionary comprehension
Do list comprehension
Create a virtual environment
Create a new file
Merge two lists
Extract items from two different lists into one list
Check if a text file is empty
Randomly select an item from a list
Break a while loop
Create a pandas DataFrame from a dictionary
Create a pandas DataFrame from a list
Get the last item of a list
Delete a column from a pandas dataframe
Access a column of a pandas dataframe
Create a class
Make a webpage request
Get the first two characters of a string
Loop through two lists at the same time
Recommended Course

Learn Flask development and learn to build cool apps with our premium Python course on Udemy.


Mailing List

We will keep you Python-motivated if you subscribe below.