In this post, let’s write a simple Python program to print random numbers. To print random numbers it’s very simple in Python as it has a random package available in the Python core itself.
Here is the simple program:
1 2 3 4 5 6 7 |
# Import random package import random # Let's print random numbers between 0 and 100 rand_num = random.randint(0,100) print (rand_num) |
The above program will print random numbers between 0 and 100.
Let’s write the same program getting the inputs from the user:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Import random package import random # Get the input from user print ("Enter the start range: ") num1 = input() print ("Enter the end range: ") num2 = input() # Let's print random numbers between 0 and 100 rand_num = random.randint(num1,num2) print ("The random number is:") print (rand_num) |
Example Output of the above program is:
1 2 3 4 5 6 |
Enter the start range: 1 Enter the end range: 1000 The random number is: 733 |
I hope this post would have helped you to learn basic Python programming.