File indexing completed on 2025-01-18 09:51:10
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
0018 #define BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
0019
0020 #include <iosfwd>
0021 #include <boost/assert.hpp>
0022 #include <boost/random/detail/config.hpp>
0023 #include <boost/random/detail/operators.hpp>
0024
0025 namespace boost {
0026 namespace random {
0027
0028
0029
0030
0031
0032
0033
0034 template<class RealType = double>
0035 class bernoulli_distribution
0036 {
0037 public:
0038
0039
0040
0041 typedef int input_type;
0042 typedef bool result_type;
0043
0044 class param_type
0045 {
0046 public:
0047
0048 typedef bernoulli_distribution distribution_type;
0049
0050
0051
0052
0053
0054
0055 explicit param_type(RealType p_arg = RealType(0.5))
0056 : _p(p_arg)
0057 {
0058 BOOST_ASSERT(_p >= 0);
0059 BOOST_ASSERT(_p <= 1);
0060 }
0061
0062
0063 RealType p() const { return _p; }
0064
0065
0066 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
0067 {
0068 os << parm._p;
0069 return os;
0070 }
0071
0072
0073 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
0074 {
0075 is >> parm._p;
0076 return is;
0077 }
0078
0079
0080 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
0081 { return lhs._p == rhs._p; }
0082
0083
0084 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
0085
0086 private:
0087 RealType _p;
0088 };
0089
0090
0091
0092
0093
0094
0095
0096 explicit bernoulli_distribution(const RealType& p_arg = RealType(0.5))
0097 : _p(p_arg)
0098 {
0099 BOOST_ASSERT(_p >= 0);
0100 BOOST_ASSERT(_p <= 1);
0101 }
0102
0103
0104
0105 explicit bernoulli_distribution(const param_type& parm)
0106 : _p(parm.p()) {}
0107
0108
0109
0110
0111
0112
0113 RealType p() const { return _p; }
0114
0115
0116 bool min BOOST_PREVENT_MACRO_SUBSTITUTION () const
0117 { return false; }
0118
0119 bool max BOOST_PREVENT_MACRO_SUBSTITUTION () const
0120 { return true; }
0121
0122
0123 param_type param() const { return param_type(_p); }
0124
0125 void param(const param_type& parm) { _p = parm.p(); }
0126
0127
0128
0129
0130
0131 void reset() { }
0132
0133
0134
0135
0136
0137 template<class Engine>
0138 bool operator()(Engine& eng) const
0139 {
0140 if(_p == RealType(0))
0141 return false;
0142 else
0143 return RealType(eng() - (eng.min)()) <= _p * RealType((eng.max)()-(eng.min)());
0144 }
0145
0146
0147
0148
0149
0150 template<class Engine>
0151 bool operator()(Engine& eng, const param_type& parm) const
0152 {
0153 return bernoulli_distribution(parm)(eng);
0154 }
0155
0156
0157
0158
0159 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, bernoulli_distribution, bd)
0160 {
0161 os << bd._p;
0162 return os;
0163 }
0164
0165
0166
0167
0168 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, bernoulli_distribution, bd)
0169 {
0170 is >> bd._p;
0171 return is;
0172 }
0173
0174
0175
0176
0177
0178 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(bernoulli_distribution, lhs, rhs)
0179 { return lhs._p == rhs._p; }
0180
0181
0182
0183
0184
0185 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(bernoulli_distribution)
0186
0187 private:
0188 RealType _p;
0189 };
0190
0191 }
0192
0193 using random::bernoulli_distribution;
0194
0195 }
0196
0197 #endif