using System;
class RndBox {
public RndBox()
{
random = new Random();
available = false;
}
/* move to the next element of the pseudo random
sequence */
public void Next()
{
if (available)
available = false;
else
generate();
}
/* pseudo random deviate from the gaussian
distribution */
public double LastGaussian()
{
return available ? first : second;
}
// generate two pseudo random deviates from a
// gaussian distribution
private void generate()
{
randpoint();
double p = Math.Sqrt((-2.0 * Math.Log(product)) / product);
first = p * x;
available = true;
second = p * y;
}
// a point from a uniform distribution on the area
// of the unit circle
private void randpoint()
{
do
{
x = randuniv() * 2 - 1;
y = randuniv() * 2 - 1;
product = x * x + y * y;
} while (product > 1.0);
}
private double x, y, product, first, second;
private bool available;
// internal wrapper for uniform distribution on
// unit interval
private double randuniv()
{
return random.NextDouble();
}
private Random random;
} |