]> git.donarmstrong.com Git - rsem.git/blob - boost/random/bernoulli_distribution.hpp
Added posterior standard deviation of counts as output if either '--calc-pme' or...
[rsem.git] / boost / random / bernoulli_distribution.hpp
1 /* boost random/bernoulli_distribution.hpp header file
2  *
3  * Copyright Jens Maurer 2000-2001
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)
7  *
8  * See http://www.boost.org for most recent version including documentation.
9  *
10  * $Id: bernoulli_distribution.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
11  *
12  * Revision history
13  *  2001-02-18  moved to individual header files
14  */
15
16 #ifndef BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
17 #define BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
18
19 #include <cassert>
20 #include <iostream>
21 #include <boost/random/detail/config.hpp>
22
23 namespace boost {
24
25 /**
26  * Instantiations of class template \bernoulli_distribution model a
27  * \random_distribution. Such a random distribution produces bool values
28  * distributed with probabilities P(true) = p and P(false) = 1-p. p is
29  * the parameter of the distribution.
30  */
31 template<class RealType = double>
32 class bernoulli_distribution
33 {
34 public:
35   // In principle, this could work with both integer and floating-point
36   // types.  Generating floating-point random numbers in the first
37   // place is probably more expensive, so use integer as input.
38   typedef int input_type;
39   typedef bool result_type;
40
41   /** 
42    * Constructs a \bernoulli_distribution object.
43    * p is the parameter of the distribution.
44    *
45    * Requires: 0 <= p <= 1
46    */
47   explicit bernoulli_distribution(const RealType& p_arg = RealType(0.5)) 
48     : _p(p_arg)
49   {
50     assert(_p >= 0);
51     assert(_p <= 1);
52   }
53
54   // compiler-generated copy ctor and assignment operator are fine
55
56   /**
57    * Returns: The "p" parameter of the distribution.
58    */
59   RealType p() const { return _p; }
60   /**
61    * Effects: Subsequent uses of the distribution do not depend
62    * on values produced by any engine prior to invoking reset.
63    */
64   void reset() { }
65
66   /**
67    * Returns: a random variate distributed according to the
68    * \bernoulli_distribution.
69    */
70   template<class Engine>
71   result_type operator()(Engine& eng)
72   {
73     if(_p == RealType(0))
74       return false;
75     else
76       return RealType(eng() - (eng.min)()) <= _p * RealType((eng.max)()-(eng.min)());
77   }
78
79 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
80   /**
81    * Writes the parameters of the distribution to a @c std::ostream.
82    */
83   template<class CharT, class Traits>
84   friend std::basic_ostream<CharT,Traits>&
85   operator<<(std::basic_ostream<CharT,Traits>& os, const bernoulli_distribution& bd)
86   {
87     os << bd._p;
88     return os;
89   }
90
91   /**
92    * Reads the parameters of the distribution from a @c std::istream.
93    */
94   template<class CharT, class Traits>
95   friend std::basic_istream<CharT,Traits>&
96   operator>>(std::basic_istream<CharT,Traits>& is, bernoulli_distribution& bd)
97   {
98     is >> std::ws >> bd._p;
99     return is;
100   }
101 #endif
102
103 private:
104   RealType _p;
105 };
106
107 } // namespace boost
108
109 #endif // BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP