endMy function called DicePlot, simulates rolling 10 dice 5000 times. In the function, it calculates the sum of values of the 10 dice of each roll, which will be a 1 × 5000 vector, and plot relative frequency histogram with edges of bins being selected in the same manner where each bin in the histogram should represent a possible value of for the sum of the dice.
function DicePlot
NumDice = 2;
NumFace = 6;
NumRoll = 500;
AllRoll = randi(NumFace, NumRoll, NumDice);
SumRoll = sum(AllRoll, 2);
Bins = (NumDice:NumFace * NumDice)';
hist(SumRoll, Bins);
title(sprintf('Histogram generated from %d rolls of %d %d-sided dice', NumRoll, NumDice, NumFace));
xlabel(sprintf('Sum of %d dice', NumDice));
ylabel('Count');
hold on
Mu = mean(SumRoll);
Sigma = sqrt(var(SumRoll));
LB = Mu - 4 * Sigma; UB = Mu + 4 * Sigma;
Partition = (LB:(UB - LB) / 100:UB)';
GaussianData = normpdf(Partition, Mu, Sigma);
GaussianData = NumRoll * GaussianData;
plot(Partition, GaussianData, '-r');
hold off