% nDiceSum.m
% Suppose you have n number of dice & you throw them m number of times.
% In each throw you record the total sum of the dice.
% At the end of m trials, find the maximum value of the sums.
% Since this maximum value can come up more than once,
% produce a table showing the trial number where such a sum came up,
% and show the values of the individual die of the trial.
% An example of such an experiment is shown below.
% K. Ming Leung, October 10, 2008.

%%% 40 pts total

clear all;
nDice = 4;
nTrials = 300;
DICE = ceil( 6 * rand(nDice, nTrials) );    % 5
Sum = sum( DICE, 1);                        % 5
maxSum = max( Sum );                        % 5
IsEqualMax = (Sum == maxSum);               % 5
Indices = find ( IsEqualMax );              % 5
nTimes = length( Indices) ;                 % 5

Header = '        Trial#  ';
DieString = [];
for n = 1:nDice
    DieString = [ DieString 'die' num2str(n) '  ' ];
end
Header = [ Header DieString ] ; 
format compact;
disp(Header);                                           % 5
disp( [ (1:nTimes)' Indices' (DICE(:,IsEqualMax))' ] )  % 5

%%% AN EXAMPLE:
%
% DICE =
%      5     4     6     2     1     1     1     2     2     3
%      4     1     3     4     4     3     2     1     4     4
%      6     1     6     4     3     2     1     4     5     3
% 
% Sum =
%     15     6    15    10     8     6     4     7    11    10
%
% maxSum =
%     15
% 
%%% OUTPUT:
%
%         Trial#  Die1  Die2  Die3 
%      1     1     5     4     6
%      2     3     6     3     6





