Home | About | Categories | All Tips & Tutorials

Learning Python3, Generate a random number using Python3

ID: 225

Category: Python3

Added: 8th of December 2020

Views: 1,817

The following code below generates a random number from 1 to 10 using Python3.

To generate a random number, we must first import the random module, by entering import random at the top of our script.

We can generate a random number by using the random.randit() function and assign it to the variable random_number

After this we use the print command to display the random number.

Copy and paste the code below

#!/usr/bin/python3
import random

random_number = random.randint(1,10)
print(random_number)


Save the file then enter the following command to make it executable on Linux
chmod +x random_number.py


Then run the file by entering the following in the terminal
./random_number.py