% ordersta.m
% This MATLAB code simulates tosses of a coin with probability p
% of getting a Head and 1-p of getting a Tail and counts the time
% until the kth Head occurs. You are prompted to supply
% the probability p, the number k, and the maximum number of tosses.
% At the end, output will be a vector giving the times til
% the kth Head occurs. A total of 200 simulations are made.
% A histogram summarizes the data, as well as the mean and
% variance being calculated
%
!c:
n=input('maximum times to toss the coin: ')
p=input('probability of heads on a single toss: ')
k=input('count time until what number of Heads occur: ')
% nh will count the number of heads, nt the number of tails
x=[ ];
for j=1:200
nh=0;
nt=0;
i=0;
while nh < k,
i=i+1;
a=rand(1,1);
if a <= p
nh=nh+1;
else
nt=nt+1;
end;
if i >= n
i=k+1000;
nh=k;
else;
end;
end;
x=[x i];
end;
m=mean(x);
s=std(x);
md=median(x);
disp('mean, median and standard deviation of time to ');
k;
disp('occurrence of head are ');
m,md,s,pause
hist(x)
title('histogram of time distribution'),pause