TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Python

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
Featued image for: How to Generate a Random Number in Python
Feature image by Catherine from Pixabay.
Random numbers are a tricky thing, especially when what you need is true randomness. Unfortunately, within the world of a computer, true randomness isn’t actually possible. Why? Computers can only do what they are told. To achieve true randomness, you would have to introduce an outside influence to make it happen (such as moving the mouse, typing random characters on a keyboard, the wind, etc.). However, pseudo-randomness is possible and that’s what we’re talking about. Using random numbers within a Python program can be very useful. For example, let’s say you play Dungeons & Dragons and would like to create a small Python program to roll numbers for you. Sure, rolling physical dice brings so much satisfaction to the player, but creating a fun little app for this is a great way to learn about using random numbers in Python. Let me show you how. It starts with the random module.

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
Let me first demonstrate generating a random number with a very simple app that, when run, will generate a single random number between a specific range.

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
Our next line will define n using the randint() function like so:
n = random.randint(0,10)
Notice we don’t just use n = randint(0,10) because we have to tell Python what randint() is a part of. Finally, we tell Python to print n with:
print(n)
The above line will print out the result of randint(0,10). The entire app looks like this:
import random

n = random.randint(0,10)
print(n)
Create the new file with:
nano simplerand.py
Paste the above code into the file and save it. Run the application with:
python3 simplerand.py
Every 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
Our next line defines number_1 using the input() function like so:
number_1 = input("Enter the number of sides on your dice from 3, 6, 10, 20, and 100: ")
The next line is a bit tricky because we have to make sure Python knows the first number in our range is always 0 (Because you have to allow for critical misses, right?). This line looks like this:
n = random.randint(0,int(number_1))
So we set n as a random number ranging from 0 to whatever the user inputs (which was defined with number_1). Finally, we tell Python to print the results with the line:
print(n)
Our entire program looks like this:
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)
Create the new file with:
nano rando.py
Run the app with:
python3 rando.py
Each 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)
When you run the above code, it will keep asking for input and spitting out a random number. To end the app, hit the Ctrl+C keyboard shortcut and you’re ready. And that’s simple random number generation in Python.
Group Created with Sketch.
TNS owner Insight Partners is an investor in: Bit.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.