% Monte Carlo approximation of pi

L= 10;      % length of square (diameter of circle)
N= 3000;    % total number of darts

figure      % Open new plot window
hold on     % don't erase old points
axis equal on

hits= 0;    % number of darts in the circle so far
for k = 1:N
    % Throw kth dart
    x= rand()*L - L/2;
    y= rand()*L - L/2;

    if x^2 + y^2 <= (L/2)^2
        hits= hits + 1;
        plot(x, y, 'b*')
    else
        plot(x, y, 'r*')
    end
end

myPi= 4*hits/N

hold off    % restore default plotting behavior
