]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/random/test/test_bernoulli.cpp
3 * Copyright Steven Watanabe 2011
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
12 #include <boost/random/bernoulli_distribution.hpp>
13 #include <boost/random/uniform_int.hpp>
14 #include <boost/random/uniform_01.hpp>
15 #include <boost/random/mersenne_twister.hpp>
16 #include <boost/math/distributions/binomial.hpp>
17 #include <boost/lexical_cast.hpp>
18 #include <boost/exception/diagnostic_information.hpp>
23 #include "chi_squared_test.hpp"
25 bool do_test(double p
, long long max
) {
26 std::cout
<< "running bernoulli(" << p
<< ")" << " " << max
<< " times: " << std::flush
;
28 boost::math::binomial
expected(static_cast<double>(max
), p
);
30 boost::random::bernoulli_distribution
<> dist(p
);
33 for(long long i
= 0; i
< max
; ++i
) {
34 if(dist(gen
)) ++count
;
37 double prob
= cdf(expected
, count
);
39 bool result
= prob
< 0.99 && prob
> 0.01;
40 const char* err
= result
? "" : "*";
41 std::cout
<< std::setprecision(17) << prob
<< err
<< std::endl
;
43 std::cout
<< std::setprecision(6);
48 bool do_tests(int repeat
, long long trials
) {
50 boost::uniform_01
<> rdist
;
52 for(int i
= 0; i
< repeat
; ++i
) {
53 if(!do_test(rdist(gen
), trials
)) {
58 std::cout
<< "*** " << errors
<< " errors detected ***" << std::endl
;
64 std::cerr
<< "Usage: test_bernoulli_distribution -r <repeat> -t <trials>" << std::endl
;
69 bool handle_option(int& argc
, char**& argv
, char opt
, T
& value
) {
70 if(argv
[0][1] == opt
&& argc
> 1) {
73 value
= boost::lexical_cast
<T
>(argv
[0]);
80 int main(int argc
, char** argv
) {
82 long long trials
= 1000000ll;
89 if(argv
[0][0] != '-') return usage();
90 else if(!handle_option(argc
, argv
, 'r', repeat
)
91 && !handle_option(argc
, argv
, 't', trials
)) {
99 if(do_tests(repeat
, trials
)) {
105 std::cerr
<< boost::current_exception_diagnostic_information() << std::endl
;