summaryrefslogtreecommitdiffstats
path: root/libcxx
diff options
context:
space:
mode:
authorMarshall Clow <mclow.lists@gmail.com>2014-09-17 18:33:58 +0000
committerMarshall Clow <mclow.lists@gmail.com>2014-09-17 18:33:58 +0000
commit3c71bff667743c328fa1b7f0cc11392a204846ca (patch)
treee387416bdf0eeafa40a8d8ac241db8edf4811176 /libcxx
parentfe357c003f3a7c23aef0176d18eb22f0c5b0cd70 (diff)
downloadbcm5719-llvm-3c71bff667743c328fa1b7f0cc11392a204846ca.tar.gz
bcm5719-llvm-3c71bff667743c328fa1b7f0cc11392a204846ca.zip
Fix PR#20843: binomial_distribution<unsigned> is broken. Add test to ensure that signed and unsigned verstions produce the same sequence.
llvm-svn: 217976
Diffstat (limited to 'libcxx')
-rw-r--r--libcxx/include/random5
-rw-r--r--libcxx/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bin/eval.pass.cpp11
2 files changed, 15 insertions, 1 deletions
diff --git a/libcxx/include/random b/libcxx/include/random
index c0db1abacc0..e7053ce4ecf 100644
--- a/libcxx/include/random
+++ b/libcxx/include/random
@@ -4009,6 +4009,8 @@ binomial_distribution<_IntType>::param_type::param_type(result_type __t, double
}
}
+// 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>::operator()(_URNG& __g, const param_type& __pr)
if (__u < 0)
return __rd - 1;
}
- --__rd;
+ if ( __rd != 0 )
+ --__rd;
++__ru;
if (__ru <= __pr.__t_)
{
diff --git a/libcxx/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bin/eval.pass.cpp b/libcxx/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bin/eval.pass.cpp
index 3bfd2af6106..85ff2ac4750 100644
--- a/libcxx/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bin/eval.pass.cpp
+++ b/libcxx/test/numerics/rand/rand.dis/rand.dist.bern/rand.dist.bern.bin/eval.pass.cpp
@@ -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;
OpenPOWER on IntegriCloud