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

import math
import random
import csv
rolls= int(input('How many dice rolls?  '))
result = []
for i in range (0, rolls):
  count = random.randint(1,6)
  result.append(count)

res = str(input('Do you want to show the results? Enter "yes" or "no" '))
if(res == 'yes'):
  print('\n RESULTS:')
  row = math.ceil(rolls/ 10)
  for j in range (0, row):
    line=str(result[j*10:j*10+10])
    print (line)

with open('die.csv', 'a+', encoding='utf-8') as csv_file:
  csvwriter = csv.writer(csv_file)
  csvwriter.writerow(result)

print('\n')

print('Number of ones   :  ', result.count(1))
print('Number of twos   :  ', result.count(2))  
print('Number of threes :  ', result.count(3))
print('Number of fours  :  ', result.count(4))
print('Number of fives  :  ', result.count(5))
print('Number of sixes  :  ', result.count(6))

⬆️⬆️⬆️ See in Google Colaboratory


HOW THE CODE WORKS?

  1. We import math libraries (for additional calculations), random (for random simulation) and csv (for generating a .csv file
  2. The user is prompted to enter the number of throws to simulate.
  3. We create an empty list „result” to store roll results.
  4. In a loop for a certain number of flips is simulated.
  5. 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„.
  6. 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.
  7. The drawn numbers are saved to the CSV file „cube.csv”, which is opened in the append mode with UTF-8 encoding.
  8. The program displays the number of each of the six possible cube values ​​drawn using the function count in the result list.