function count = rollDice(n,faces,rolls)
% Simulate rolling of n fair dice
% Usage: count = rollDice(n, faces, rolls)
% N is the number of dice
% FACES is vector of the number of faces on each die
% faces(i) is the number of faces on the ith die
% ROLLS is the number of times to roll die
% COUNT is vector of number of times each outcome occurs
% count(t) is the number of times outcome t occurs

maxOut= sum(faces); % highest possible outcome from rolling all dice
count= zeros(1,maxOut); % bins to store counts

for i = 1:rolls
% Roll the n dice
outcome= 0; % total outcome from all n dice
for d = 1:n
outcome= outcome + ceil(rand()*faces(d));
end
% Increment the correct bin
count(outcome)= count(outcome) + 1;
end

% Show histogram of outcome
bar(n:maxOut, count(n:end));
message= sprintf('Outcomes from %d rolls of %d fair dice', rolls, n);
title(message);
xlabel('Outcome'); ylabel('Count');