%HTsimulation
%Peter wins every time a head turns up
%Peter is in the lead when his winnings are positive.When Peters winnings are 0,...
%he is in the lead if he was ahead at the previous toss
%%
clc;clear all;close all;
tosses=40;
out=rand(tosses,1);
heads=out<0.5;
tails=heads-1;
earnings=[0;cumsum(heads+tails)];
lead=0;
for i=1:tosses
if earnings(i+1)>0 || (earnings(i+1)==0 && earnings(i)>0)
lead(i+1)=lead(i)+1;
else
lead(i+1)=lead(i);
end
end
plot(0:tosses,earnings,0:tosses,lead,'r');grid;
xlabel('Number of tosses');legend('Earnings (pennies)','Peter in lead');
title('Peter wins a penny everytime a head turns up and loses one otherwise');
%%
clc;clear all;
tosses=40;
experiments=10000;
out=rand(tosses,experiments);
heads=out<0.5;
tails=heads-1;
earnings=sum(heads+tails);
figure;
hist(earnings,-tosses:2:tosses);grid;
xlabel('Pennies earned');ylabel('Percentage of Occurence');
title('Win Distribution');
%%
%Lead problem
earnings=[zeros(1,experiments); cumsum(heads+tails)];
for i=1:experiments
lead(i)=0;
for j=1:tosses
if earnings(j+1,i)>0 || (earnings(j+1,i)==0 && earnings(j,i)>0)
lead(i)=lead(i)+1;
else
lead(i)=lead(i);
end
end
end
figure;
hist(lead,0:2:tosses);grid;
xlabel('No of times in lead');ylabel('Percentage of Occurence');
title('Lead Distribution');