Super Simple Python is a series dedicated to creating super simple Python projects for beginners to be able to do in under 15 minutes. In this episode, we’ll be covering building a Python random number generator in under 10 lines of code!
Video version here:
We’ll begin by importing the random
module. This is a native Python library.
import random
Next, we’ll tell the user what the program does with a print
statement.
print("This program generates a random number between two given numbers")
Now we’ll ask the user for the two numbers that they wish for our random number generator to generate a number between. Notice that we wrap the input in an int()
statement. Input is a string by default, but we want an integer.
low = int(input("What is the lower limit? "))
high = int(input("What is the higher limit? "))
Finally, we’ll generate a number between those two numbers, low
, and high
using the randint
function from the random
library and print it out for the user to see. The f
in the print statement in front of the string allows us to directly pass variables in using brackets as we do with num
.
num = random.randint(low, high)
print(f"Your randomly generated number is: {num}")
When we run the final program, it should output something like the image below:
To learn more feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Python skills!
I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
DonateDonate monthlyDonate yearly
6 thoughts on “Super Simple Python: Random Number Generator”