function monteCarlo
%  This example involves running a 
%  monte-carlo simulation that produces 
%  an integer from 1 to 20.  We want to
%  display a histogram of the result.

% Copyright 2008 The MathWorks, Inc.

N = 500000;
nCoins = 20;
numHeads = zeros(1, N);

tic

parfor (simNum = 1:N)
     numHeads(simNum) = flipCoins(nCoins);
end

toc

x=(0:20);
figure, hist(numHeads,x);


function counter = flipCoins(nCoins)
% Simulate the flipping of 20 coins and count the number of heads 
counter = sum(rand(1, nCoins) > .5);


