In: Computer Science
PYTHON CODING:
“Roll” 2 dice 10,000 times keeping track of all the sums of each set of rolls in a list. Then use your program to generate a histogram summarizing the rolls of two dice 10,000 times.
from __future__ import division
import random
# to get a random number
def dice_roll():
return random.randint(1, 6)
# this function calculates the total number of ooccurences of all the possible outcome
def simulate(number_of_times):
counter = {n : 0 for n in range(2, 13)}
for i in range(number_of_times):
first_dice = dice_roll()
second_dice = dice_roll()
total = first_dice + second_dice
counter[total] += 1
return counter
#total number of rolls
rolls = 10000
counter = simulate(rolls)
total = sum(counter.values())
li=[]
# printing the count and probablity of occurence of all possible values
print("The count and probablity of occurence of values: \n")
for total, count in counter.items():
print("{} - {} {:0.4f}%".format(total, count, count / rolls))
val = round(count / rolls , 4)
li.append(val)
import matplotlib.pyplot as plt
import numpy as np
# all possible outcomes
sample = [2,3,4,5,6,7,8,9,10,11,12]
# printing the plot showing probablities of all possible outcomes
x_axis = list(set(sample))
plt.bar(x_axis, li)
plt.ylabel('Probability');
PLEASE LIKE THE SOLUTION :))
IF YOU HAVE ANY DOUBTS PLEASE MENTION IN THE COMMENT