function [Pexact,Pnumber,Odds]=crapsmof65(number,H)
% format [Pexact,Pnumber,Odds]=crapsmof65(number,H)
% input: number=value of 1st roll
%        H    ='H' for Pnumber & odds of making 4, 6, 8, or 10 the hard way
% output: Pexact= Exact probability of winning, given 1st roll = number
%         Pnumber=Probability of rolling that number by rolling 2 dice.
%         Odds   =Odds of making number = Pexact/(1-Pexact), NaN if
%                 Pexact=1
% Using the 'method of reduced sample space' from Mosteller, F. 1965. Fifty
%   challenging problems in probability with solutions, p. 9
% see craps.m for a more laborious solution using absorbing Markov chains
% written 6/2/03 by E. Gallagher for EEOS601, revised 1/15/11;
% http://alpha.es.umb.edu/faculty/edg/files/edgwebp.html
if nargin > 1 
    if H=='H'
        if mod(number,2) | number ==2 | number==12 
            error(...
              'Can only make even numbers between 4 & 10 the ''hard'' way')
            return
        end
    end
end
die=1:6; % create a sample space of two dice rolls
die1=repmat(die,6,1); % create a matrix, with each row equal to 1 to 6;
die=die';  % transpose of die, now a column vector
die2=repmat(die,1,6); % create a matrix, with each column equal to 1 to 6;
sumdice=die1+die2;  % This gives the sample space, the 36 sums of two die.
i=find(sumdice==number);  % find the indices for each value equal to number;
lengthi=length(i);     % find how many sums of 2 die equal the number in input;
if lengthi==0          % only true if the number isn't in the sample space.
    disp('Your number isn''t possible, not in the sample space')
    number
    % The following odd syntax will print the number
    fprintf('Your number, %6.0f, isn''t possible, not in the sample space\n',number)
    return % This stops the program after displaying the message;
end
[r,c]=size(sumdice); % find size of sample space
Pnumber=lengthi/(r*c); % What is the probability of rolling your number with 1 roll
                       % of two dice
% Is the craps shooter an immediate winner?
if (number==7 | number==11) % 2 logical statements using the | OR command.
    Pexact=1;              % Matlab performs statements in this if section only
                            % if the logical statement number equals 7 or equals 11
                            % is true.
    Odds=NaN;
    return
end
% Is the craps shooter an immediate loser?
if (number==2 | number==3 | number==12)
    Pexact=0;
    Odds=0;
    return  % exit the function and return to the command window.
end
% Exact probability of making a point - no need for sum of geometric series
% or Markov chains
% Use Mosteller's 'Method of reduced sample space: 
% all we need is Probability of rolling a given
% number - which wins - and the probability of rolling a 7, which loses.
%  We don't need any of the other probabilities 
% (e.g., probability of rolling 2, 3 ...)
%  First find probability of rolling a 7, which results in a loss.
j=find(sumdice==7);P7=length(j)/(r*c); % P7 is the probability of rolling 7
if nargin>1 & H=='H'
    ih=find(sumdice==number & die1==number/2);
    lengthih=length(ih);
    Pnumberh=lengthih/(r*c); % Another way to lose is to roll a soft number
    Pexact=Pnumberh/(P7+Pnumber); % You win or lose if you roll an 8 
else
    Pexact=Pnumber/(Pnumber+P7);
end
Odds=Pexact/(1-Pexact);