Talk:Marsaglia polar method

From Wikipedia, the free encyclopedia
Jump to: navigation, search
WikiProject Statistics (Rated Start-class, Low-importance)
WikiProject icon

This article is within the scope of the WikiProject Statistics, a collaborative effort to improve the coverage of statistics on Wikipedia. If you would like to participate, please visit the project page or join the discussion.

Start-Class article Start  This article has been rated as Start-Class on the quality scale.
 Low  This article has been rated as Low-importance on the importance scale.


This page calls the method the "Marsaglia polar method" without offering any justification. I was worried about that and did a little research; it looks to me as if it *is* correct to credit it to Marsaglia; here are some notes.

According to Box-Muller transform, the method is attributed to Marsaglia by Devroye's book cited there. Chapter 5 of that book does indeed say that the method is due to Marsaglia (page 236, first line) but gives no citation. One of that book's references is to a paper by Marsaglia and Bray in SIAM Review 6 (1964) called "A convenient method for generating normal random variables". That's about an entirely different method, but it mentions the polar rejection method in passing on the first page. (Actually, the entirely different method uses the rejection method as a fallback in rare cases.)

In the references of Ziggurat algorithm there's a paper from ACM Computing Surveys 39 (2007) called "Gaussian random number generators" which attributes the method to Knop with a date of 1969. Seems like Marsaglia (and Bray?) has priority. Curiously, the ACMCS paper goes on to discuss the Marsaglia-Bray paper immediately after crediting someone else with an algorithm found in that very paper five years earlier!

I don't know whether the paper by Marsaglia and Bray is the first appearance of this algorithm in the literature. Its common attribution to Marsaglia alone suggests that perhaps it isn't.

Gareth McCaughan (talk) 18:10, 31 January 2008 (UTC)

Hi, i was thinking that this polar method (with rejection outside of unit circle) was developed by Box and Muller. Witek —Preceding unsigned comment added by (talk) 17:19, 11 December 2009 (UTC)

According to survay paper "Gaussian Random Number Generators" by David B. Thomas, Wayne Luk, Philip H.W.Leong and John D. Villasenor, Polar method was invented by Bell 1968 and Knop 1969. Witek. —Preceding unsigned comment added by (talk) 16:15, 28 January 2010 (UTC)

Errors in C code?[edit]

The C code seems to contain some errors. It scales U and V by the variance (standard deviation squared), whereas it should scale U and V by the standard deviation, right? That's what the Java code does. Also, why is a pointer to the variance being passed? Also, are the bool and qreal datatypes part of standard C? I'm happy to fix these problems, but I wanted to make sure that they are problems. Mgnbar (talk) 21:41, 23 April 2014 (UTC)

The variance parameter is even crazier than I said above. Anyway, here is a proposed fix. Online commentators suggest that rand() is not a good random number generator. So I've abstracted out that detail, and used drand48() instead. Mgnbar (talk) 12:37, 24 April 2014 (UTC)

#include <stdlib.h>

double sampleUniformDistribution(double a, double b) {
    return a + (b - a) * drand48();

double sampleNormalDistribution(const double mean, const double stddev) {
    static int hasSpare = 0;
    static double spare;
    if (hasSpare) {
        hasSpare = 0;
        return mean + stddev * spare;
    else {
        hasSpare = 1;
        static double u, v, s, root;
        do {
            u = sampleUniformDistribution(-1.0, 1.0);
            v = sampleUniformDistribution(-1.0, 1.0);
            s = u * u + v * v;
        } while (s >= 1.0 || s == 0.0);
        root = sqrt(-2.0 * log(s) / s);
        spare = v * root;
        return mean + stddev * u * root;

Why do we need static variables here?

        static double u, v, s, root;

As I understand, they are not used in next function calls. Static word makes compiler to locate them in memory but it is enough to keep them in processor registers. Also such solution creates multi-threading troubles. — Preceding unsigned comment added by (talk) 07:51, 9 January 2015 (UTC)