function [] = BoxMuller(NumPts);
% Demonstrates the spiral effect when uniform variates are generated using
% an LCG. The LCG used is one that would not be used in practice, so that
% the spirals can be easily seen.
% (Note that if x(n+1) = ( a x(n) + c ) mod b, then there will be a spirals
% in the picture.)
% argument = Num of Pts to generate: 5000 is reasonable

a = 7;
b = 2^31;

ThePts = zeros(2 * NumPts, 1);
ThePts(1) = 2^30 - 1;
for n = 1: 2 * NumPts - 1,
  ThePts(n+1) = rem(a * ThePts(n), b);
end

ThePts = ThePts / b;

x = zeros(NumPts, 1);
y = zeros(NumPts, 1);
for n = 1: NumPts,
  indx = 2 * n - 1;
  x(n, 1) = sqrt(-2 * log(ThePts(indx))) * cos(2 * pi * ThePts(indx+1));
  y(n, 1) = sqrt(-2 * log(ThePts(indx))) * sin(2 * pi * ThePts(indx+1));
end

plot(x, y, 'g.');
