%   Mysterious Transfer
%   Content of a box denoted as a pair [#Black balls,#White balls]
%   Contents at start: first box [8,4],  second [4,6], third [2,2]
%   Problem: From 1st box a ball is randomly selected and transferred
%   to the 2nd box, then from the 2nd box a ball is selected and transferred
%   to the 3rd box. Then, from the 3rd box a ball is selected: Find an
%  approximation to the probability that the ball from the 3rd box was black? 
%
clear allc
close all


  disp('Misterious Transfer')
  lw = 2; 
  set(0, 'DefaultAxesFontSize', 16);
  fs = 15;
  msize = 10; 
  rand('twister',0)  
%



M=100000; %# of experiments
nb=0; %# of black balls selected
%
content1=[8 4];
for i=1:M
first = randsample('BW',1, true, content1); %draw 1st
if (first =='B') content2 =[5 6];
else content2=[4 7];
end
second = randsample('BW',1, true, content2); %draw 2nd
if (second =='B') content3 =[3 2];
else content3=[2 3];
end
third = randsample('BW',1,true,content3); %draw 3rd
if (third == 'B')    nb = nb+1;
end

end
disp(['The prob of ball from the 3rd box black ', num2str(nb/M,5)]);
%
% exact 0.4867
%