Python Program to Generate Random Numbers

« Previous Program Next Program »

Generate Random Numbers in Python

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 given below.

Python Programming Code to Generate Random Numbers

Following python program ask from user to enter lower and upper limit then to enter amount of numbers to generate and print the required number of random numbers:

# Python Program - Generate Random Numbers
from random import randint
while True:
	print("Enter 'x' for exit.")
	val1 = input("Enter lower limit: ")
	val2 = input("Enter upper limit: ")
	val3 = input("How many random numbers to print ? ")
	if val1 == 'x':
		lower = int(val1)
		upper = int(val2)
		amount = int(val3)
		for i in range(0, amount):
			print(randint(lower, upper),end=" ")

Here is the sample run of the above python program to illustrate how to generate and print random numbers:

python generate random numbers

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:

« Previous Program Next Program »

Latest 5 Posts by CodesCracker


Quick Links
Signup - Login - Give Online Test