]> AND Private Git Repository - canny.git/blob - stc/exp/ml_stc_linux_make_v1.0/include/boost/random/binomial_distribution.hpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
952142fa3cb407a4c7923d6cec38519ffc45cada
[canny.git] / stc / exp / ml_stc_linux_make_v1.0 / include / boost / random / binomial_distribution.hpp
1 /* boost random/binomial_distribution.hpp header file\r
2  *\r
3  * Copyright Jens Maurer 2002\r
4  * Distributed under the Boost Software License, Version 1.0. (See\r
5  * accompanying file LICENSE_1_0.txt or copy at\r
6  * http://www.boost.org/LICENSE_1_0.txt)\r
7  *\r
8  * See http://www.boost.org for most recent version including documentation.\r
9  *\r
10  * $Id: binomial_distribution.hpp 52492 2009-04-19 14:55:57Z steven_watanabe $\r
11  *\r
12  */\r
13 \r
14 #ifndef BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP\r
15 #define BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP\r
16 \r
17 #include <boost/config/no_tr1/cmath.hpp>\r
18 #include <cassert>\r
19 #include <boost/random/detail/config.hpp>\r
20 #include <boost/random/bernoulli_distribution.hpp>\r
21 \r
22 namespace boost {\r
23 \r
24 // Knuth\r
25 template<class IntType = int, class RealType = double>\r
26 class binomial_distribution\r
27 {\r
28 public:\r
29   typedef typename bernoulli_distribution<RealType>::input_type input_type;\r
30   typedef IntType result_type;\r
31 \r
32   explicit binomial_distribution(IntType t_arg = 1,\r
33                                  const RealType& p_arg = RealType(0.5))\r
34     : _bernoulli(p_arg), _t(t_arg)\r
35   {\r
36     assert(_t >= 0);\r
37     assert(RealType(0) <= p_arg && p_arg <= RealType(1));\r
38   }\r
39 \r
40   // compiler-generated copy ctor and assignment operator are fine\r
41 \r
42   IntType t() const { return _t; }\r
43   RealType p() const { return _bernoulli.p(); }\r
44   void reset() { }\r
45 \r
46   template<class Engine>\r
47   result_type operator()(Engine& eng)\r
48   {\r
49     // TODO: This is O(_t), but it should be O(log(_t)) for large _t\r
50     result_type n = 0;\r
51     for(IntType i = 0; i < _t; ++i)\r
52       if(_bernoulli(eng))\r
53         ++n;\r
54     return n;\r
55   }\r
56 \r
57 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS\r
58   template<class CharT, class Traits>\r
59   friend std::basic_ostream<CharT,Traits>&\r
60   operator<<(std::basic_ostream<CharT,Traits>& os, const binomial_distribution& bd)\r
61   {\r
62     os << bd._bernoulli << " " << bd._t;\r
63     return os;\r
64   }\r
65 \r
66   template<class CharT, class Traits>\r
67   friend std::basic_istream<CharT,Traits>&\r
68   operator>>(std::basic_istream<CharT,Traits>& is, binomial_distribution& bd)\r
69   {\r
70     is >> std::ws >> bd._bernoulli >> std::ws >> bd._t;\r
71     return is;\r
72   }\r
73 #endif\r
74 \r
75 private:\r
76   bernoulli_distribution<RealType> _bernoulli;\r
77   IntType _t;\r
78 };\r
79 \r
80 } // namespace boost\r
81 \r
82 #endif // BOOST_RANDOM_BINOMIAL_DISTRIBUTION_HPP\r