Dice Roller
INTRODUCTION
The roll of the dice is an experiment. As a result of rolling, we can get one of the numbers from 1 to 6, so the sample space will be:
$S= \{1, 2, 3, 4, 5, 6 \}$
Each of the numbers is equally likely to be obtained, so if we roll the dice 600 times, each of the numbers 1 through 6 should statistically come up 100 times.
We can generate the result of rolling a dice multiple times In Python.
PYTHON CODE
HOW THE CODE WORKS?
- We import math libraries (for additional calculations), random (for random simulation) and csv (for generating a .csv file
- The user is prompted to enter the number of throws to simulate.
- We create an empty list âresultâ to store roll results.
- In a loop for a certain number of flips is simulated.
- In each draw, using the function randint from the module random, a random integer between 1 and 6 is generated and added to the list âresultâ.
- The user is asked if he wants to display the drawn numbers. If the user enters âyesâ, the program will display the drawn numbers in rows of 10.
- The drawn numbers are saved to the CSV file âcube.csvâ, which is opened in the append mode with UTF-8 encoding.
- The program displays the number of each of the six possible cube values ââdrawn using the function count in the result list.