% Larsen & Marx Example 2.4.11, page 59 in 4th edition 
% 3rd edition Example 2.06.11, page 63 Larsen & Marx, 3rd ed.
% Eugene D. Gallagher, written 1991, revised 1/14/11
% Three chips are placed in an urn. One is Red on both sides, a second
% is blue on both sides and a third is red on one side and blue on the
% other. One chip is selected at random and placed on a table. It is
% Red What is the probability that the color underneath is also red?
% Monte Carlo simulation of process:
coins=['RR';'RB';'BB'];
numredt=0;
numredb=0;
trials=1e3;
for i=1:trials
    pick_coin=randi(3);     % picks 1, 2, or 3 randomly
    pick_face=randperm(2);   % permutes [1 2] randomly
    if coins(pick_coin,pick_face(1))=='R'
        numredt=numredt+1;    
        if coins(pick_coin,pick_face(2))=='R'
            numredb=numredb+1;
        end
    end
end
P=numredb/numredt
Exp=2/3;
fprintf('Expected P=%6.4f and a Monte Carlo simulation with %7.0f \n',...
    Exp,trials)
fprintf('trials found P = %6.4f\n',P)


