Skip to content

Instantly share code, notes, and snippets.

Created February 10, 2019 16:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save niyumard/6b4d5352532365e2381485d44d37035e to your computer and use it in GitHub Desktop.
Draw chart for rolling dice for k times
import random
import matplotlib.pyplot as plt
#How many times do you want the loop to run?
howmanytimes = 1000000
k = 4
def dice():
r = ''
for j in range(0,k):
r += str(random.randint(1,6))
return r
x = []
for i in range(0,howmanytimes):
d = {}
for i in range(k,k*6+1):
d[i] = 0
for i in x:
t = 0
for j in range(0,len(i)):
t += int(i[j])
d[t] += 1
for i in d:
d[i] = d[i]/len(x)
sumofprobabilities = 0
for i in d:
sumofprobabilities += d[i]
expectedvalue = 0
for i in d:
expectedvalue += d[i]*i
print("Sum of Probabilities:", sumofprobabilities)
print("Expected Value:", expectedvalue), list(d.values()), align='center')
plt.xticks(range(len(d)), list(d.keys()))
plt.suptitle('If we roll the dice '+str(k)+' times')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment