Fix portability bug in get_normal_pair (RAND_MAX != MAX_RANDOM_VALUE).

Also try to improve readability and performance.
This commit is contained in:
Tom Lane 2002-09-14 19:32:54 +00:00
parent 49c86099f3
commit d3ebc1ae4a
1 changed files with 15 additions and 19 deletions

View File

@ -224,31 +224,27 @@ get_normal_pair(float8 *x1, float8 *x2)
v2,
s;
for (;;)
do
{
u1 = (float8) random() / (float8) RAND_MAX;
u2 = (float8) random() / (float8) RAND_MAX;
u1 = (float8) random() / (float8) MAX_RANDOM_VALUE;
u2 = (float8) random() / (float8) MAX_RANDOM_VALUE;
v1 = (2.0 * u1) - 1.0;
v2 = (2.0 * u2) - 1.0;
s = pow(v1, 2) + pow(v2, 2);
s = v1 * v1 + v2 * v2;
} while (s >= 1.0);
if (s >= 1.0)
continue;
if (s == 0)
{
*x1 = 0;
*x2 = 0;
}
else
{
*x1 = v1 * sqrt((-2.0 * log(s)) / s);
*x2 = v2 * sqrt((-2.0 * log(s)) / s);
}
return;
if (s == 0)
{
*x1 = 0;
*x2 = 0;
}
else
{
s = sqrt((-2.0 * log(s)) / s);
*x1 = v1 * s;
*x2 = v2 * s;
}
}