Re: C-faq q13.20



"Thomas Lumley" <thomas@xxxxxxxxxxx> writes:

One of the examples in q13.20 in the FAQ
(http://c-faq.com/lib/sd16.html, linked from
http://c-faq.com/lib/gaussian.html) seems to be wrong.

The full code is at that link. The part of the code that actually does
the work is

y = (double) rand() / (RAND_MAX + 1.0); /* 0.0 <= y < 1.0 */
bin = (y < 0.5) ? 0 : 1;
y = fabs(y - 1.0); /* 0.0 < y <= 1.0 */
y = std_dev * sqrt((-2.0) * log(y));

return bin ? (mean + y) : (mean - y);

Indeed. The above will not produce a distribution that is symmetric
about "mean" because the decision as to which side of the mean we
return is correlated to the size of the uniform random number y. One
needs either another random sample:

return rand() > RAND_MAX/2 ? mean + y : mean - y;

or (if the your source in random bits is rather precious) the decision
must be based on a bit or bits in the original sample that are not
correlated to the size of the original y:

int r = rand();
y = (double)(r + 1) / (RAND_MAX + 1);
y = std_dev * sqrt(-2 * log(y));
return r & 1 ? mean + y : mean - y;

I can't see the advantage of the fabs. I think the above allows y to
be as small as (randomly) possible whist remaining > 0. Of course,
this last version raises the question of the suitability of using the
bottom bits returned by rand() which was well thrashed out about a
year ago.

I should add that even then I don't think the result is normally
distributed (though it will at least be symmetrical).

--
Ben.
.



Relevant Pages

  • Re: C-faq q13.20
    ... snip ... ... decision must be based on a bit or bits in the original sample ...
    (comp.lang.c)
  • Re: A test for randomness?
    ... you do not know the mean and standard deviation ... still safe in assuming he has a random sample because, in a broad way, ... the 14 sized sample fits the expected distribution. ...
    (sci.stat.math)
  • Re: A test for randomness?
    ... you do not know the mean and standard deviation ... still safe in assuming he has a random sample because, in a broad way, ... the 14 sized sample fits the expected distribution. ... If the consequnces are severe, worry, or demand ...
    (sci.stat.math)
  • Re: statistics question for a business research course
    ... >> You draw a random sample of 300 employee records from the personnel file ... >curve) distribution is usually assumed, ... parameter 10/7 and shape parameter 4.41. ...
    (sci.math)
  • Re: A test for randomness?
    ... still safe in assuming he has a random sample because, in a broad way, ... the 14 sized sample fits the expected distribution. ... Person B argues that this is a non-sequitir... ...
    (sci.stat.math)