Re: C-faq q13.20
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Sun, 19 Nov 2006 11:14:20 +0000
Ben Bacarisse <ben.usenet@xxxxxxxxx> writes:
"Thomas Lumley" <thomas@xxxxxxxxxxx> writes:<other stuff of mine snipped>
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.
I should add that even then I don't think the result is normally
distributed (though it will at least be symmetrical).
Drat! Posted by mistake. After writing, I decided that the original
linked code was so broken (this last phrase of mine is far too week --
the result is *definitely* not Gaussian) that it was best to say
nothing!
Anyway, having said more the nothing, the best thing to say is to
suggest the link be removed lest anyone try to use it. (And don't use
the above unless you want some strange, unspecified, symmetric
distribution or numbers!)
--
Ben.
.
- References:
- C-faq q13.20
- From: Thomas Lumley
- Re: C-faq q13.20
- From: Ben Bacarisse
- C-faq q13.20
- Prev by Date: Re: C-faq q13.20
- Next by Date: Re: wall clock time in milliseconds to time c code sections
- Previous by thread: Re: C-faq q13.20
- Next by thread: Re: C-faq q13.20
- Index(es):
Relevant Pages
|