* get_normal_pair() * Assigns normally distributed (Gaussian) values to a pair of provided * parameters, with mean 0, standard deviation 1. * * This routine implements Algorithm P (Polar method for normal deviates) * from Knuth's _The_Art_of_Computer_Programming_, Volume 2, 3rd ed., pages * 122-126. Knuth cites his source as "The polar method", G. E. P. Box, M. E. * Muller, and G. Marsaglia
| 280 | * |
| 281 | */ |
| 282 | static void |
| 283 | get_normal_pair(float8 *x1, float8 *x2) |
| 284 | { |
| 285 | float8 u1, |
| 286 | u2, |
| 287 | v1, |
| 288 | v2, |
| 289 | s; |
| 290 | |
| 291 | do |
| 292 | { |
| 293 | u1 = (float8) random() / (float8) MAX_RANDOM_VALUE; |
| 294 | u2 = (float8) random() / (float8) MAX_RANDOM_VALUE; |
| 295 | |
| 296 | v1 = (2.0 * u1) - 1.0; |
| 297 | v2 = (2.0 * u2) - 1.0; |
| 298 | |
| 299 | s = v1 * v1 + v2 * v2; |
| 300 | } while (s >= 1.0); |
| 301 | |
| 302 | if (s == 0) |
| 303 | { |
| 304 | *x1 = 0; |
| 305 | *x2 = 0; |
| 306 | } |
| 307 | else |
| 308 | { |
| 309 | s = sqrt((-2.0 * log(s)) / s); |
| 310 | *x1 = v1 * s; |
| 311 | *x2 = v2 * s; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * crosstab - create a crosstab of rowids and values columns from a |