% Box Muller algorithm verification
nsamples = 1000000;

% generate two independent uniform distributed random variables
x1 = rand(nsamples,1);	
x2 = rand(nsamples,1);

% transform the distributions
f = sqrt(-log(x1));
g = sqrt(2.0)*cos(2*pi*x2);

%gaussian distribution
n = f.*g;	

% plot histogram
hist(n,500)

% standard deviation is 1.0
std(n)

% mean is zero
mean(n)


