function Aufgabe3
%Aufgabe3 - Simulate Monty-Hall-Problem and plot empirical evidence.
%
%usage
%  Aufgabe3
%
%description
%  Aufgabe3 simulates the Monty-Hall-Problem 10000 times and plots are pie-chart
%  which indicates whether it is wise to switch doors or not.
%
%author
%  buenau@cs.tu-berlin.de

N = 10000;

who_won = zeros(2, N);

for i=1:N
  % Put price behind a random door.
  price = zeros(1,3);
  price(randint(1, 1, [1 3])) = 1;
 
  % Strategy 1: Choose door at random. 
  strat1_sel = randint(1, 1, [1 3]); 

  % Strategy 2: Switch to other door.   
  host_opens = randomchoice(setdiff(1:3, [ strat1_sel find(price) ]));
  strat2_sel = randomchoice(setdiff(1:3, [ strat1_sel host_opens ]));

  who_won(1, i) = price(strat1_sel);
  who_won(2, i) = price(strat2_sel);
end

pie(sum(who_won, 2) / N, { 'Strategy 1: Do not change', 'Strategy 2: Change doors' });
title(sprintf('Frequency of wins for the two strategies in %d simulations', N));

% Choose an element of vector a at random.
function c = randomchoice(a)
c = a(randint(1, 1, [1 length(a)]));
