Python program to generate random number

Program to generate random number between 1 and 6 with practical example.

Program Description:

In this program,we have used random module to generate random number. randint() method is used to generate sequence of random number

Program Logic:

  • Import random module in program using import statement
  • Use randint function of random module to generate random number and store it in variable ‘n’
  • Ask user to enter number between 1 to 6 using input method
  • Use if condition to check whether given number is random number or not
  • If given condition is TRUE then print Congratulation message on screen.
  • If given condition is FALSE then print Sorry message on screen

Below is implementation code/source code

import random
n = random.randint(1,6)
guess = int(input("Enter a number between 1 to 6:"))
if n == guess:
else :
    print("Sorry,Try again\n Original number is",n)

Note : we may get different output because this program generates random number in range 0 and 6. 


Enter a number between 1 to 6:2
Sorry,Try again
Original number is 5

Enter a number between 1 to 6:1

Below is Snapshot of Executable code with Output

python program to generate random number

Related Python program

  1. Python Program to read text file and find out longest word from file
  2. Python Program to count number of words in a file
  3. Python program to write those lines which have the character ‘P’ from one text file to another text file
  4. Python program to get file size in bytes