Skip to Content
Course content

431: Building a Dice Rolling Simulator

Click on the "Edit" button in the top corner of the screen to edit your slide content.

How do I actually get a random number that feels like a die?

When you're building a simulator, the first thing you'll reach for is the random module. Now, you have a few choices here, but for a dice roll, random.randint(a, b) is your best friend. The reason is simple: unlike most Python functions where the stop value is exclusive (like range()), randint is inclusive.

If you want a standard six-sided die, you use random.randint(1, 6). This ensures you can actually hit that 6, which would be a problem in a game of Yahtzee if your code only ever gave you 1 through 5.

import random

def roll_die():
    return random.randint(1, 6)

print(f"You rolled a: {roll_die()}")

How do I keep the simulator running without restarting the script?

If you just run the code above, it rolls once and dies. That's annoying. In the real world, we handle this with a while loop. I usually prefer a while True loop combined with a specific "exit" keyword. It makes the program feel like an actual application rather than a one-off script.

You'll want to capture user input inside the loop. I like to use .lower() on the input just in case the user types "QUIT" instead of "quit"β€”it's a small touch, but it prevents the program from crashing or ignoring the user just because of a Shift key.

import random

while True:
    user_input = input("Press Enter to roll or type 'q' to quit: ").lower()
    if user_input == 'q':
        print("Thanks for playing!")
        break
    
    result = random.randint(1, 6)
    print(f"🎲 You rolled a {result}!")

What if I want to support different types of dice, like a D20?

Hardcoding the number 6 is fine for a basic example, but if you're building this for a tabletop RPG, you're going to need flexibility. The trick here is to stop treating the die as a fixed object and start treating it as a parameter in a function.

I recommend creating a function that accepts the number of sides as an argument. This way, the same logic handles a 4-sided die, a 20-sided die, or even a 100-sided die without you having to write three different functions. I've seen beginners write separate functions for roll_d6() and roll_d20()β€”don't do that. Keep it DRY (Don't Repeat Yourself).

import random

def roll_custom_die(sides):
    if sides < 1:
        return "A die must have at least one side!"
    return random.randint(1, sides)

# Now you can just call it with whatever you need
print(f"D20 Roll: {roll_custom_die(20)}")
print(f"D4 Roll: {roll_custom_die(4)}")



πŸ“‹ Practical Task

Build a Tabletop RPG Multi-Dice Roller

Instead of rolling a single die, create a program that allows a user to simulate multiple dice of the same size at once (e.g., "3d6" meaning three six-sided dice).

  • Ask the user for the number of dice they want to roll.
  • Ask the user for the number of sides each die should have.
  • Use a loop to generate the rolls and store them in a list.
  • Print each individual roll and the final total sum of all dice.
  • Wrap the entire process in a loop so the user can keep rolling until they type 'exit'.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.