VOOZH about

URL: https://thenewstack.io/how-to-generate-a-random-number-in-python/

⇱ How to Generate a Random Number in Python - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
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%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-02-26 17:00:39
How to Generate a Random Number in Python
tutorial,
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 Jack Wallen
👁 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.
TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Bit.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.