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);
The code is supposed to generate Gaussian random variables, but with
mean=0 and std_dev=1 the smallest number it can generate is
sqrt(-2*log(0.5)), about -1.177. The distribution is two-humped, and
so not even vaguely Gaussian.
In the interests of making this message more c.l.c-conforming I should
probably also add a coding quibble:
bin = (y < 0.5) ? 0 : 1;
should just be
bin = y<0.5;
-thomas