Learn practical skills, build real-world projects, and advance your career
# Dice Rolling Example 

import matplotlib.pyplot as plt
import numpy

q = numpy.array([])
samps = int(1e6)
for k in range(0, samps):
    X = numpy.random.randint(1, 6 + 1, 2)
    q = numpy.append(q, numpy.sum(X))

m = numpy.cumsum(q) / numpy.arange(1, samps + 1)
plt.figure(1)
plt.semilogx(numpy.arange(1, samps + 1), m)
plt.xlabel("N")
plt.ylabel("Average Sum")
plt.grid()
plt.show()

# print(m[samps - 1])
plt.figure(2)
plt.hist(q, 11, density=True)
plt.xlabel("Sum")
plt.ylabel("Frequency")
plt.show()

print(numpy.sum(q >= 8) / samps)
Notebook Image
0.4166