function pi = piNumber(n)
% Finds number pi using Monte Carlo method: chooses random points
% in the square 0<(x,y)<1 and counts how many obey x^2+y^2<1. This
% must be the ratio of one fourth the area of the circle of unit
% radius (pi/4) to the area of the square (one).
% Input
%   n  : Number of Monte Carlo trial points
% Output
%   pi : Estimated value of pi
% Usage
%   pi = piNumber(1000)
% Author
%   Jose M Soler. Nov.2010

r = rand(2,n);       % n random points in the unit square
r2 = sum(r.^2);      % sums only over first index
nInside = sum(r2<1); % number of points inside circle: (r2<1) is a vector
pi = 4*nInside/n;    %    of 0's (false) and 1's (true)
end
