Build your Own Dice Simulator in Python

Build your Own Dice Simulator in Python

Hello, Guys on this tutorial you’re going to learn out how to build your own Roll dice simulator game in python, which simulate the randomness of the die with its six faces using python builtin random number generator.

You can complete this ...

This is a beginner tutorial, you just need to know basic concepts of python such as variables, if statement, and loop.

Structure of our program

We are going to use the builtin python random module to generate a random number ranging from 1 to 6 equivalent to the faces of the die and then prompt the user to guess the number generated.

Basics random module

Let's have a look at a python random module, If you're already familiar with then that's great else let us get some basics

In order to generate a simulated dice random number, we use the randint() function where we specify the min value and max value for our random generation, since our dice have 6 faces probably the range is going to be starting from 1 to 6.

Example of Usage

>>> import random
>>> random.randint(1, 6)
5
>>> random.randint(1, 6)
2

Very simple indeed as shown above now, we just have to use the while loop to make our game continuous.

Full code for Our Dice Simulator

You can take it as a challenge by building your own dice simulator using concepts of random number generation and while loop to build your own or you can take a look at my code below to see how I did it.

Have a look

from random import randint 
​
while True:
    dice_random = randint(1, 6)
    print('Print Rolled dice ', dice_random)
    print('Should I generate again  ?')
    again = int(input('1.Yes\n2.No\n\nEnter option here : ')) 
    if again != 1:
        break
print('Thanks for playing the game')

Note :

I didn't set any prompt for the user to gues the number, I'm assuming they will use it on playing the physical game.

Output

When you run the above code, it should lead you to an output similar to what shown below

$ python3 app.py 
Print Rolled dice  1
Should I generate again  ?
1.Yes
2.No
​
Enter option here : 1
Print Rolled dice  6
Should I generate again  ?
1.Yes
2.No
​
Enter option here : 1
Print Rolled dice  3
Should I generate again  ?
1.Yes
2.No
​
Enter option here : 2
Thanks for playing the game

Hope you find it interesting, In case of any suggestion or comment, drop it in the comment box and I will get back to you ASAP.

I also recommend you checking