n = 10000;

%% (a) simulate n times rolling a die:
maxeyes = 8;
x = rollthedie(n, maxeyes);

%% (b) probability P(5):
P5 = sum(x == 5)/length(x);
fprintf('P(5)=%.3f, expected is %.3f\n', P5, 1/maxeyes);

for i =1:maxeyes
    P = sum(x == i)/length(x);
    fprintf('P(%d)=%.3f, expected is %.3f\n', i, P, 1/maxeyes);
end

%% (c) P(i)
P = zeros(1, maxeyes);
for i =1:maxeyes
    P(i) = sum(x == i)/length(x);
end
subplot(1, 2, 1)
plot([0 maxeyes+1], [1/maxeyes 1/maxeyes], 'r', 'linewidth', 3)
hold on
bar(P);
hold off
set(gca, 'XTick', 1:maxeyes);
xlim([0 maxeyes+1]);
xlabel('Eyes');
ylabel('Probability');

%% (d) histogram of x
subplot(1, 2, 2);
diehist(x);

%% (e) loaded die
% eye 1 to 5 have P=1/8, eye 6 has P = 3/8 !
x = randi(8, 1, n);   % random numbers from 1 to 8
x(x>6) = 6;           % set numbers 7 and 8 to 6
diehist(x);
savefigpdf(gcf, 'die1.pdf', 12, 5)