Python Dice Rolling Simulator using Random Module

Ever wondered how any online dice game generates a number on the dice randomly? Seems like the application generates a number randomly and automatically under the hood and the game proceeds without any unfair means due to the random generating concept. These randomly generated numbers are generated using the Random Module in Python. So let’s get started and learn about this module and create a Dice Rolling Simulator Project using Python.

What is a Dice Rolling Simulator?

A dice rolling simulator is an application that generates a number on the dice automatically when a button is triggered. It works just like a real-life dice. This application is used in several games like Ludo, Snake and Ladders, etc.

Python Dice Rolling Simulator Project Details

This is a very interesting project developed using Random Module and Tkinter Module. The Random Module used helps in generating pseudo numbers and Tkinter Module helps in creating GUI in Python.
The code is explained in detail below.

Python Random Module

Random Module generates pseudo-random numbers.

The numbers generated by the Random Module have a specific range when it comes to the Integers and you can also limit the random generated to a certain range according to your conveniences.

Python Tkinter Module

Tkinter Module is useful to create the fastest and easiest GUI using Python.

Download Python Dice Rolling Simulator

Please download the source code of python dice rolling simulator project: Dice Rolling Simulator Python Project Code

Project Prerequisites

To implement the following code you need to install Tkinter Module and Random Module. These modules will be used in the project throughout.

pip install tk
pip install random

Steps to Create Dice Rolling Simulator in Python

Let us have a look at the steps to create the Dice Rolling Simulator Project-
1. Install and import the required modules.
2. Create a blank simulator window.
3. Give the appropriate heading and create a button that when triggered randomly generates a number on the dice.
4. Create a function that implements the random generation of a number when we click the button.

1. Importing the Required Modules:

from tkinter import *
import random
  • Importing both Tkinter and Random module for using them further in the project.
  • from tkinter import * – means import everything from the tkinter module.

2. Creating the blank Simulator Window:

simulator_window = TK()
simulator_window.minsize(300,300) # for setting the minimum width and height for our window
simulator_window.maxsize(300,300) # for setting the maximum width and height for our window
simulator_window.title('TechVidvan')# giving window a appropriate title
  • simulator_window = TK() – for creating a window.
  • Creating this window to put in the button and required headings.

3. Creating Heading and Button:

heading = Label(simulator_window,text='ROLL THE DICE',font=('bold',20),bg='light grey') # This will create a label for the heading.
heading.pack() # You have to pack this label so that it can be seen in the window
  • heading =Label() – creates a label named heading to place the heading in the window.
  • heading.pck() – to pack the label in the simulator window.
button = Button(simulator_window,text='Roll',font=('normal',20),command=roll, bg='yellow')
button.pack()# button which when triggered performs rolling of dice

Creating a button that when triggered performs the roll() function.

  • button=Button() – creates a button named button with specific background colour, font and text to display.
  • button.pack() – packs and displays the button on the simulator window.
  • command= roll – specifies that when the button is clicked then the roll function is implemented.

4. Create the roll() function:

def roll():#function to roll the dice
min = 1
max = 6
l =Label(simulator_window, text = random.randint( min,max), font=('bold', 100))# result that will be displayed when the button is clicked
l.place(x=110, y=100)# placing the label

Defining the roll() function with a def keyword and creating two variables min and max to set the boundary of the randit() function.

  • l = Label() creates a label that displays the result of the roll() function on the simulator window.
  • random.randit(min,max) – randint() is a function that generates random numbers from the given range of numbers. Here the range is min to max.
  • l.place() – places the label in the simulator window.

5. Opening the Main Window

stimulator_window.mainloop()

This code line opens the main window which displays the output.

Well done! You have now successfully completed the project. Let us have a look at the output.

Python Dice Rolling Simulator Output

When you click on the Roll button, a number is randomly generated. In the output above, the number 5 is displayed as the random number.

python dice rolling simulator output

Summary

We successfully created the Dice Rolling Simulator Project using Python. While creating the project we learned about Tkinter Module and Random Module and different functions associated with it. Now we can use this Dice Rolling Simulator in any game that we build. Hope you enjoyed building this project with us.