%  Simulate tossing a coin

n=1000 % number of tosses
p=.5 % probability of heads
toss=rand(1,n)<p;
heads=sum(toss)