How to Generate a Random Number in Python
Getting ready for your next Dungeons & Dragons tournament? Create a small Python program to roll the numbers for you.
Feb 26th, 2024 5:00pm by
Feature image by Catherine from Pixabay.
What Is the Random Module?
The Python random module is built into Python (so you don’t have to install it after installing Python itself) and includes several methods, such as:- seed() – initializes a random number generator.
- getstate() – returns the current state of the random number generator
- randrange() – returns a random number between a given range (exclusive of the endpoint)
- randint() – returns a random number between a given range (inclusive of both endpoints)
- choice() – returns a random element from a given sequence
- shuffle() – shuffles a sequence of numbers and returns them in a random order
What You’ll Need
The only thing you’ll need for this is a desktop or laptop computer that has Python3 installed. This can be Linux, MacOS, or Windows. Of course, you’ll also need a text editor. I’ll demonstrate this on Ubuntu Budgie and the nano editor. If you use a different platform (or a different editor), you’ll need to alter the file creation command. Other than that, you should be able to follow along without any problems.A Simple Random Number Generator
For our first app, we’re going to use randint() to generate a random number between 0 and 10. The first thing in our app is to import the random module with:
import random
n = random.randint(0,10)
print(n)
import random
n = random.randint(0,10)
print(n)
nano simplerand.pyPaste the above code into the file and save it. Run the application with:
python3 simplerand.pyEvery time you run the program, you should see a pseudo-random number between 0 and 10. If you wanted to not include the outer ranges of our limit, you’d use the randrange() function. By using this function, you’d exclude 0 and 10 as options from the randomness. That code would look like this:
import random
n = random.ranrange(0,10)
print(n)
A Bit More Complicated Random Number Generator
Of course, the above application isn’t very handy, especially when you’re getting your D&D on. You might have to roll a 3, 6, 10, 20, or 100-sided dice. Let me show you how easy that can be to create. For this, we’re going to accept input from a user and tell them to enter the sides of the dice required. Of course, the first thing we do is import random with:
import random
number_1 = input("Enter the number of sides on your dice from 3, 6, 10, 20, and 100: ")
n = random.randint(0,int(number_1))
print(n)
import random
number_1 = input("Enter the number of sides on your dice from 3, 6, 10, 20, and 100: ")
n = random.randint(0,int(number_1))
print(n)
nano rando.pyRun the app with:
python3 rando.pyEach time you run the app, you’ll get a pseudo-random result ranging from 0 to whatever number the user typed. A fun spin on this is to add a while loop into the mix, such that you don’t have to keep running the program over and over to see the roll of the dice. That code looks like this:
import random
while True:
number_1 = input("Enter the number of sides on your dice from 3, 6, 10, 20, and 100: ")
n = random.randint(0,int(number_1))
print(n)
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.