Dice Roll Generator in Python

Dice Roll Generator in Python

3 mins read726 Views Comment
Updated on Apr 30, 2024 10:27 IST

Creating a dice roll generator in Python is a great way to get started with the language. Not only does it give you the opportunity to work with some fundamental programming concepts, but it also allows you to create a fun and interactive program that you can play with yourself or share with others. In this article, we’ll take a look at how to create a dice roll generator in Python step-by-step, so that you can follow along and create your own.

dice roll generator in python

 

In this article, we are going to build a classic dice roll generator using Python. We will be writing two programs for this –

So, without further ado, let’s get started!

For the first program, we will be using the random module of Python to randomize the generated outputs simulating the throw of a dice.

Basic Dice Roll Generator Python Program

To understand the following program, you should have basic knowledge of the following Python concepts:

  • Python Random Module
  • Python Loops

Here, we will use the randint() method of the random module. This function will generate a random number in the specified range. Let’s look at the Python implementation below:


 
<code class="codehighlight" lang="py><code>">
Copy code

Output

2022_07_image-133.jpg

What have we done here?

  • We first import the random module of Python.
  • Then we specify the min and max variables that correspond to the minimum and maximum value on a dice respectively, i.e., 1 and 6.
  • Next, we create a while loop that runs every time the dice is rolled. We will make use of the random.randint() method to generate a random number between 1-6. This method works because all 6 faces of dice have an equal probability of occurring when the dice is rolled.

Thus, we have created a basic dice roll generator program that can be useful for Python beginners to help them think logically and give them an idea of how more advanced algorithms are written in Python.

Must Read: What is Python?

Must Check: Python Online Course & Certification

Next, we are going to build yet another dice roll generator but this time, with the Python Tkinter package.

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks

What is Tkinter?

Tkinter is the standard Graphical User Interface (GUI) library in Python. It is a fast and easy-to-use package that provides a powerful object-oriented interface for designing GUI applications in Python.

Let’s how we will build our dice roll generator application using Tkinter:

Dice Roll Generator Application Using Tkinter

To understand the following program, you should have basic knowledge of the following Python concepts:

  • Python Random Package
  • Python Tkinter package
  • Python Loops

Here, we will develop a computer application that will roll the dice for us.

  • Step 1: Download and install the Tkinter package. Execute the following command in your terminal:

 
pip install tkinter
Copy code
  • Step 2: Import the random and Tkinter packages:

 
import random
import tkinter
Copy code
  • Step 3: Create the GUI:

 
root = tkinter.Tk()
root.geometry('400x400')
root.title('Shiksha Online Python Roll Dice')
#Label to print result
label = tkinter.Label(root, text='', font=('Consolas', 260))
#Label to introduce
label2 = tkinter.Label(root, text='Welcome to Shiksha Online Python Dice roll. Click to roll the dice!', font=('Consolas',10))
label2.place(x=150,y=400)
Copy code

What have we done here?

In this step, we have created a Tk root widget to build the main window of our dice roll application using the following functions:

  • title() function to give a title to the app.
  • label() function to create a label that can make text appear on the GUI.
  • Step 4: Define a dice roll function:

 
def roll_dice():
value = ['⚀', '⚁', '⚂', '⚃', '⚄', '⚅']
result=random.choice(value)
label.configure(text=result)
label.pack()
if(result=='⚀'):
label3=tkinter.Label(root,text='You rolled a 1 ! Roll the dice again? ',font=('Consolas',10))
label3.place(x=150,y=450)
elif(result=='⚁'):
label3=tkinter.Label(root,text='You rolled a 2 ! Roll the dice again? ',font=('Consolas',10))
label3.place(x=150,y=450)
elif(result=='⚂'):
label3=tkinter.Label(root,text='You rolled a 3 ! Roll the dice again? ',font=('Consolas',10))
label3.place(x=150,y=450)
elif(result=='⚃'):
label3=tkinter.Label(root,text='You rolled a 4 ! Roll the dice again? ',font=('Consolas',10))
label3.place(x=150,y=450)
elif(result=='⚄'):
label3=tkinter.Label(root,text='You rolled a 5 ! Roll the dice again? ',font=('Consolas',10))
label3.place(x=150,y=450)
elif(result=='⚅'):
label3=tkinter.Label(root,text='You rolled a 6 ! Roll the dice again? ',font=('Consolas',10))
label3.place(x=150,y=450)
Copy code

What have we done here?

In this step, we have defined a function called roll_dice() that uses an if-elif-else loop to select a random number between 1-6.

The variable value stores all the Unicode values for each face of the dice. And the variable result stores the selected random value. We then print this value using a label.

  • Step 5: Create the button:

 
button = tkinter.Button(root, text='roll dice', foreground='red', command=roll_dice)
button.pack()
root.mainloop()
Copy code

What have we done here?

In this step, we will create a button that, when clicked on, will run the function we created in step 4. For this, we will make use of the Tkinter.Button() method. This method will call the roll_dice() function.

We will also use the mainloop() function to update the GUI and wait for the events to happen.

Output:

2022_07_image-134.jpg

As you can see above, we have created our very own dice roll generator application! Let’s click on the roll dice button again:

2022_07_image-135.jpg

Looks cool, doesn’t it?

Conclusion

Hope this article proved helpful and fun in teaching you how to build a dice role generator in Python. 

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio