diff options
author | Tom Lane | 2002-09-14 19:32:54 +0000 |
---|---|---|
committer | Tom Lane | 2002-09-14 19:32:54 +0000 |
commit | d3ebc1ae4a9feee927c8fd6a6322364c2555e43d (patch) | |
tree | 4bb4cf8e00bf54ebfb53a2763b7020e19f20097b /contrib/tablefunc/tablefunc.c | |
parent | 49c86099f3bfde9f048f415a465f23ed6ac52d57 (diff) |
Fix portability bug in get_normal_pair (RAND_MAX != MAX_RANDOM_VALUE).
Also try to improve readability and performance.
Diffstat (limited to 'contrib/tablefunc/tablefunc.c')
-rw-r--r-- | contrib/tablefunc/tablefunc.c | 34 |
1 files changed, 15 insertions, 19 deletions
diff --git a/contrib/tablefunc/tablefunc.c b/contrib/tablefunc/tablefunc.c index 875e340c29e..f5ac0f8ee67 100644 --- a/contrib/tablefunc/tablefunc.c +++ b/contrib/tablefunc/tablefunc.c @@ -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; } } |