[libcxx] r217976 - Fix PR#20843: binomial_distribution<unsigned> is broken. Add test to ensure that signed and unsigned verstions produce the same sequence.

Marshall Clow mclow.lists at gmail.com
Wed Sep 17 11:33:58 PDT 2014


Author: marshall
Date: Wed Sep 17 13:33:58 2014
New Revision: 217976

URL: http://llvm.org/viewvc/llvm-project?rev=217976&view=rev
Log:
Fix PR#20843: binomial_distribution<unsigned> is broken. Add test to ensure that signed and unsigned verstions produce the same sequence.

Modified:
    libcxx/trunk/include/random
    libcxx/trunk/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bin/eval.pass.cpp

Modified: libcxx/trunk/include/random
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/random?rev=217976&r1=217975&r2=217976&view=diff
==============================================================================
--- libcxx/trunk/include/random (original)
+++ libcxx/trunk/include/random Wed Sep 17 13:33:58 2014
@@ -4009,6 +4009,8 @@ binomial_distribution<_IntType>::param_t
     }
 }
 
+// Reference: Kemp, C.D. (1986). `A modal method for generating binomial
+//           variables', Commun. Statist. - Theor. Meth. 15(3), 805-813.
 template<class _IntType>
 template<class _URNG>
 _IntType
@@ -4035,7 +4037,8 @@ binomial_distribution<_IntType>::operato
             if (__u < 0)
                 return __rd - 1;
         }
-        --__rd;
+        if ( __rd != 0 )
+            --__rd;
         ++__ru;
         if (__ru <= __pr.__t_)
         {

Modified: libcxx/trunk/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bin/eval.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bin/eval.pass.cpp?rev=217976&r1=217975&r2=217976&view=diff
==============================================================================
--- libcxx/trunk/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bin/eval.pass.cpp (original)
+++ libcxx/trunk/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bin/eval.pass.cpp Wed Sep 17 13:33:58 2014
@@ -322,6 +322,17 @@ int main()
         assert(std::abs((kurtosis - x_kurtosis) / x_kurtosis) < 0.01);
     }
     {
+        const int N = 100000;
+        std::mt19937 gen1;
+        std::mt19937 gen2;
+
+        std::binomial_distribution<>         dist1(5, 0.1);
+        std::binomial_distribution<unsigned> dist2(5, 0.1);
+
+        for(int i = 0; i < N; ++i)
+            assert(dist1(gen1) == dist2(gen2));
+    }
+    {
         typedef std::binomial_distribution<> D;
         typedef std::mt19937 G;
         G g;





More information about the cfe-commits mailing list