n = 10000; %% (a) simulate n times rolling a die: x = rollthedie( n ); %% (b) probability P(3): P3 = sum(x == 3)/length(x); fprintf( 'P(3)=%.3f, expected is %.3f\n', P3, 1/6 ); for i =1:6 P = sum(x == i)/length(x); fprintf( 'P(%d)=%.3f, expected is %.3f\n', i, P, 1/6 ); end %% (c) P(i) P = zeros(1, 6); for i =1:6 P(i) = sum(x == i)/length(x); end subplot( 1, 2, 1 ) plot( [0 7], [1/6 1/6], 'r', 'linewidth', 3 ) hold on bar( P ); hold off set(gca, 'XTick', 1:6 ); xlim( [ 0 7 ] ); 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)