# Super Simple Python: Dice Roll Simulator

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 your own dice roll simulator in under 10 lines of code.

For a video guide see

## Deciding Which Dice Rolls We Want to Simulate

Before we can build a dice roll simulator, we’ve got to decide which dice we’d like to simulate rolling. The most common dice is a d6 –  a six sided dice. Other dice that you can simulate (thank you Dungeons and Dragons for inspiration) are d4, d8, d10, d12, d20, and d100. It doesn’t really matter which ones you pick, under the hood we’re doing the same thing we did in the episode on building a random number generator or rock, paper, scissors.

# Rolling Dice in 10 Lines of Python

As always, we’ll start with the imports we need to run our program. In this case, we only need the `random` library. It’s native to Python so we don’t need to `pip install` anything.

import random

## Setting Up the Dice Dictionary

Once we have our imports, let’s create a dictionary to hold our dice ranges in. For this example I’ve decided to roll a d6, a d12, and a d20. Notice that the ranges are from 1 to whatever the highest number is. The `randint` function we’ll be using is inclusive. This isn’t the only way to save these ranges, we can simply save the highest number and always use 1 as our low. This is just how I chose to save these.

ranges = {
"d6": (1, 6),
"d12": (1, 12),
"d20": (1, 20)
}

## Rolling the Dice

Now that we have our dice dictionary set up, all we gotta do is roll the dice. To roll the dice, we’ll ask the user for input on which dice they want to roll. Let’s convert their response into lowercase so that it matches our dictionary set up. Then, we call the dictionary to get the range of the rolled dice. Finally, we print out a random integer picked from that range.

dice_size = input("Do you want to roll a d6, d12, or d20? ").lower()
_range = ranges[dice_size]
print(random.randint(_range[0], _range[1]))

When we run our program to simulate rolling dice we should get results that look like the following images:

Ouch, can’t believe we got two nat 1’s. Anyway, that’s all there is to it! In this episode of Super Simple Python we’ve covered rolling dice, stay tuned for more!

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.

One-Time
Monthly
Yearly

#### Make a yearly donation

Choose an amount

¤5.00
¤15.00
¤100.00
¤5.00
¤15.00
¤100.00
¤5.00
¤15.00
¤100.00

Or enter a custom amount

## 7 thoughts on “Super Simple Python: Dice Roll Simulator”

1. scream truly an awe-inspiring self-complacent thanks so much for communion i wish impose this groovy site every azygos daytime