public double Gamma(double a)
{
if (a <= -1) {
throw new ArgumentException("a (" + a + ") is not greater than " +
(-1));
}
double v, x, u, x2, d, c;
d = (a < 1 ? 1 + a : a) - (1.0 / 3.0);
c = 1 / Math.Sqrt(9 * d);
do {
do {
x = this.Normal();
v = Math.Pow((c * x) + 1, 3);
} while (v <= 0);
u = this.Uniform();
x2 = Math.Pow(x, 2);
} while (u >= 1 - (0.0331 * x2 * x2) &&
Math.Log(u) >= (0.5 * x2) + (d * (1 - v + Math.Log(v))));
if (a < 1) {
return d * v * Math.Exp(this.Exponential() / -a);
} else {
return d * v;
}
}