Python Program to Generate Random Numbers

Python Program to Generate Random Numbers

  • To generate random numbers in python, you have to ask from user to enter the range (enter lower and upper limit) and again ask to enter how many random numbers he/she want to print to generate and print the desired number of random numbers as shown here in the program.

Source Code:

# Python Program - Generate Random Numbers

from random import randint
print("Enter 'x' for exit.")
val1 = input("Enter lower limit to generate random numbers: ")
if val1 == 'x':
    exit()
else:
    val2 = input("Enter upper limit to generate random numbers: ")
    val3 = input("How many random numbers to print ? ")
    lower = int(val1)
    upper = int(val2)
    amount = int(val3)
    print()
    for i in range(0, amount):
        print(randint(lower, upper),end=" ")


Comments