%coints.m
% This MATLAB code simulates n tosses of a coin with probability p
% of getting a Head and 1-p of getting a Tail. You are prompted to supply
% the probability p and the number of tosses. At the end, output will
% be the number of Heads and Number of Tails which occurred, stored
% in a vector out.
%
!c:
!c:
n=input('how many times to toss the coin: ')
p=input('probability of heads on a single toss: ')
% nh will count the number of heads, nt the number of tails
nh=0;
nt=0;
x=[ ]
for i=1:n
a=rand(1,1);
if a <= p
nh=nh+1;
x=[x 1];
else
nt=nt+1;
x=[x 0];
end;
end;
per=nh/n;
out=[nh nt];
disp('number and percentage of Heads is')
nh,per,pause
disp('number and percentage of Tails is')
nt,1.-per,pause
hist(x,2)
title('histogram of H (1) and T (0)'),pause