Sandbox · Experiment
Dice Roll Histogram
This sandbox program demonstrates how random dice rolls statistically distribute. The results are displayed as a histogram, making frequencies and relative parts directly visible.

Introduction
The program simulates dice rolls and displays the results as a bar chart. Each die number from 1 to 6 gets its own bar. The higher the bar, the more frequently the corresponding number was rolled.
This creates a histogram that makes the statistical distribution of the results visible.
Central Variables
The program uses several variables to calculate statistics and display.
| Variable | Meaning |
|---|---|
counts | List of the number of rolls per die number |
total_rolls | Total number of all rolls |
MAX_BAR_HEIGHT | Maximum Height of the Bars in the Diagram |
The height of a bar is calculated relative to the most frequent die roll.
Control Panel
The simulation can be controlled both with the keyboard and with buttons in the window.
Keyboard
| Button | Function |
|---|---|
| SPACE | Single Die Roll |
| A | Series of Rolls |
| R | Reset Statistics |
| ESC | Exit Program |
Buttons
At the bottom, there are three buttons:
| Button | Function |
|---|---|
+1 | One Roll |
+10 | Ten Rolls |
RST | Reset |
The buttons perform the same functions as the keyboard commands.
How a Die Roll is Counted
A die roll is generated with a random number.
The corresponding position in the list counts is then increased.
| |
Since lists in Python start at 0, the die value is reduced by 1.
Histogram Display
The bar height is calculated proportionally to the frequency.
| |
This keeps the diagram well-scaled and independent of the number of rolls.
In addition, the program shows:
- Relative Frequency in Percent
- Estimated Expected Value
Expected Value
As soon as throws are available, an approximation of the expected value is calculated.
| |
This value approaches the theoretical value of a fair die with increasing numbers of throws.
The theoretical expected value is:
[ E = 3.5 ]
Tips for experimenting
This sandbox is intended to make your own observations.
Possible experiments:
- Throw only a few times and observe large fluctuations.
- Perform many throws and observe how the bars converge.
- Change the bar width (
bar_w) or the distance between bars. - Experiment with colors and layout.
- Try adding additional statistics or displays.
Educational significance
This sandbox connects several subject areas:
Mathematics
- Random processes
- Relative frequencies
- Expected values
Computer Science
- Lists and counter variables
- Random numbers
- Graphical representation with pygame
- Event processing
The histogram thus makes a central concept in statistics visible: Law of large numbers.