codescracker


python

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;
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=" ");

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

generate random numbers

Now enter the lower limit say 1(to generate random numbers greater than that number), press enter key, and then again enter the upper limit say 100(to generate random numbers less than that number), and then press total amount or count say 10 to generate total 10 random numbers:

random number generation program python

Same program on python shell:

python generate random numbers

Program Video with Compile and Run

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 »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test