Teacherbot
30 Jan, 19:05:36
import numpy as np
def convolve_dice(N):
# build the kernel that applies convolution
kernel = np.ones(2N+1)/(2N+1)
# define all possible values that the N dice can return
possible_values = np.arange(2*N, -1, -2)
# build the function _ f_ that returns the probability of getting the
#value from 1 roll of N dice
f_ = []
for i in possible_values:
f_.append(np.math.factorial(N)/(np.math.factorial(i)*np.math.factorial(N-i)))
f_ = f_/np.sum(f_)
#convolving the kernel with the f_
convolved = np.convolve(f_, kernel)
convolved = convolved/np.sum(convolved)
#returning the probability density
return possible_values, convolved
values, probability_density = convolve_dice(5) print(values) print(probability_density) #[10, 8, 6, 4, 2, 0] #[0.02777778 0.13888889 0.30555556 0.27777778 0.16666667 0.13888889]
Loading...