This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug libstdc++/48114] <random> binomial_distribution incorrect for p > .5
- From: "aaz at althenia dot net" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Mon, 14 Mar 2011 15:23:31 +0000
- Subject: [Bug libstdc++/48114] <random> binomial_distribution incorrect for p > .5
- Auto-submitted: auto-generated
- References: <bug-48114-4@http.gcc.gnu.org/bugzilla/>
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48114
--- Comment #3 from Andrey Zholos <aaz at althenia dot net> 2011-03-14 15:23:25 UTC ---
I'll add this here since it's in the same place and the test will be
similar.
For geometric_distribution(.25) the first 10 probabilities are:
sample correct
p_0 0.000 0.250
p_1 0.750 0.188
p_2 0.187 0.141
p_3 0.046 0.105
p_4 0.012 0.079
p_5 0.003 0.059
p_6 0.001 0.044
p_7 0.000 0.033
p_8 0.000 0.025
p_9 0.000 0.019
The smallest value returned is 1, but should be 0 (and d.min() is
correctly 0); and p is used as 1-p.
Fix:
--- include/c++/bits/random.h
+++ include/c++/bits/random.h
@@ -3628,8 +3628,8 @@
void
_M_initialize()
- { _M_log_p = std::log(_M_p); }
+ { _M_log_1_p = std::log(1 - _M_p); }
double _M_p;
- double _M_log_p;
+ double _M_log_1_p;
};
--- include/c++/bits/random.tcc
+++ include/c++/bits/random.tcc
@@ -1027,3 +1027,3 @@
do
- __cand = std::ceil(std::log(__aurng()) / __param._M_log_p);
+ __cand = std::floor(std::log(__aurng()) / __param._M_log_1_p);
while (__cand >= __thr);
Also, there's a line in random.h
_GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0)
&& (_M_p <= 1.0));
but the standard only requires 0 < p < 1.