123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- #ifndef BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
- #define BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
- #include <iosfwd>
- #include <boost/assert.hpp>
- #include <boost/random/detail/config.hpp>
- #include <boost/random/detail/operators.hpp>
- namespace boost {
- namespace random {
- template<class RealType = double>
- class bernoulli_distribution
- {
- public:
-
-
-
- typedef int input_type;
- typedef bool result_type;
- class param_type
- {
- public:
- typedef bernoulli_distribution distribution_type;
-
- explicit param_type(RealType p_arg = RealType(0.5))
- : _p(p_arg)
- {
- BOOST_ASSERT(_p >= 0);
- BOOST_ASSERT(_p <= 1);
- }
-
- RealType p() const { return _p; }
-
- BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
- {
- os << parm._p;
- return os;
- }
-
- BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
- {
- is >> parm._p;
- return is;
- }
-
- BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
- { return lhs._p == rhs._p; }
-
- BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
- private:
- RealType _p;
- };
-
- explicit bernoulli_distribution(const RealType& p_arg = RealType(0.5))
- : _p(p_arg)
- {
- BOOST_ASSERT(_p >= 0);
- BOOST_ASSERT(_p <= 1);
- }
-
- explicit bernoulli_distribution(const param_type& parm)
- : _p(parm.p()) {}
-
-
- RealType p() const { return _p; }
-
- bool min BOOST_PREVENT_MACRO_SUBSTITUTION () const
- { return false; }
-
- bool max BOOST_PREVENT_MACRO_SUBSTITUTION () const
- { return true; }
-
- param_type param() const { return param_type(_p); }
-
- void param(const param_type& parm) { _p = parm.p(); }
-
- void reset() { }
-
- template<class Engine>
- bool operator()(Engine& eng) const
- {
- if(_p == RealType(0))
- return false;
- else
- return RealType(eng() - (eng.min)()) <= _p * RealType((eng.max)()-(eng.min)());
- }
-
- template<class Engine>
- bool operator()(Engine& eng, const param_type& parm) const
- {
- return bernoulli_distribution(parm)(eng);
- }
-
- BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, bernoulli_distribution, bd)
- {
- os << bd._p;
- return os;
- }
-
- BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, bernoulli_distribution, bd)
- {
- is >> bd._p;
- return is;
- }
-
- BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(bernoulli_distribution, lhs, rhs)
- { return lhs._p == rhs._p; }
-
-
- BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(bernoulli_distribution)
- private:
- RealType _p;
- };
- }
- using random::bernoulli_distribution;
- }
- #endif
|