function N = polaralg(mu, sigma, k)

% Implementation of the polar algorithm for generating 
% normally distributed random numbers from 
% uniformly distributed random numbers.
% 
% mu and sigma are the normal distribution parameters (mean, std).
% k is the number of uniformly distributed points tested in [-1,1] x [-1,1] .
% The algorithm produces <= 2k independent, normally distributed values. 

N = []; % Initialize the list of normally distributed values.

for i = 1:k
     u = 2*rand(2,1)-1;             % Generate a uniform random point in [-1,1] x [-1,1].
     r = u'*u;                            % Compute the radius from the origin.
     if r < 1                               % Proceed only if the point is within the unit circle.
         v = sqrt(-2*log(r)/r)*u;
         n = sigma*v + mu;
         N = [N, n(1), n(2)];
     end
 end
 
 hist(N,100)