User Tools

Site Tools


gibson:teaching:fall-2012:math445:magicsack1.m
% You have a sack of nine balls, three each red, green, blue.
% If you draw two balls out of the sack, what's the chance 
% they are both green?

Ntrials = 1000; % number of trials
red   = 0;      % set a color cod. purely cosmetic!
green = 1;
blue  = 2;
sack = [red red red green green green blue blue blue]; % fill the sack!

Nballs  = length(sack);
count = 0;

for n=1:Ntrials
  
  perm = randperm(Nballs);  % random permutation of 1:6
  draw = sack(perm(1:2));   % draw the first two balls
  
  if draw(1) == green && draw(2) == green 
    count = count + 1;
  end
end

fprintf('The prob of chossing two green balls first is %f\n', count/Ntrials);
gibson/teaching/fall-2012/math445/magicsack1.m.txt · Last modified: 2012/09/27 13:53 by gibson