bernoulli_distribution.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* boost random/bernoulli_distribution.hpp header file
  2. *
  3. * Copyright Jens Maurer 2000-2001
  4. * Copyright Steven Watanabe 2011
  5. * Distributed under the Boost Software License, Version 1.0. (See
  6. * accompanying file LICENSE_1_0.txt or copy at
  7. * http://www.boost.org/LICENSE_1_0.txt)
  8. *
  9. * See http://www.boost.org for most recent version including documentation.
  10. *
  11. * $Id$
  12. *
  13. * Revision history
  14. * 2001-02-18 moved to individual header files
  15. */
  16. #ifndef BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
  17. #define BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP
  18. #include <iosfwd>
  19. #include <boost/assert.hpp>
  20. #include <boost/random/detail/config.hpp>
  21. #include <boost/random/detail/operators.hpp>
  22. namespace boost {
  23. namespace random {
  24. /**
  25. * Instantiations of class template \bernoulli_distribution model a
  26. * \random_distribution. Such a random distribution produces bool values
  27. * distributed with probabilities P(true) = p and P(false) = 1-p. p is
  28. * the parameter of the distribution.
  29. */
  30. template<class RealType = double>
  31. class bernoulli_distribution
  32. {
  33. public:
  34. // In principle, this could work with both integer and floating-point
  35. // types. Generating floating-point random numbers in the first
  36. // place is probably more expensive, so use integer as input.
  37. typedef int input_type;
  38. typedef bool result_type;
  39. class param_type
  40. {
  41. public:
  42. typedef bernoulli_distribution distribution_type;
  43. /**
  44. * Constructs the parameters of the distribution.
  45. *
  46. * Requires: 0 <= p <= 1
  47. */
  48. explicit param_type(RealType p_arg = RealType(0.5))
  49. : _p(p_arg)
  50. {
  51. BOOST_ASSERT(_p >= 0);
  52. BOOST_ASSERT(_p <= 1);
  53. }
  54. /** Returns the p parameter of the distribution. */
  55. RealType p() const { return _p; }
  56. /** Writes the parameters to a std::ostream. */
  57. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
  58. {
  59. os << parm._p;
  60. return os;
  61. }
  62. /** Reads the parameters from a std::istream. */
  63. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
  64. {
  65. is >> parm._p;
  66. return is;
  67. }
  68. /** Returns true if the two sets of parameters are equal. */
  69. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
  70. { return lhs._p == rhs._p; }
  71. /** Returns true if the two sets of parameters are different. */
  72. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
  73. private:
  74. RealType _p;
  75. };
  76. /**
  77. * Constructs a \bernoulli_distribution object.
  78. * p is the parameter of the distribution.
  79. *
  80. * Requires: 0 <= p <= 1
  81. */
  82. explicit bernoulli_distribution(const RealType& p_arg = RealType(0.5))
  83. : _p(p_arg)
  84. {
  85. BOOST_ASSERT(_p >= 0);
  86. BOOST_ASSERT(_p <= 1);
  87. }
  88. /**
  89. * Constructs \bernoulli_distribution from its parameters
  90. */
  91. explicit bernoulli_distribution(const param_type& parm)
  92. : _p(parm.p()) {}
  93. // compiler-generated copy ctor and assignment operator are fine
  94. /**
  95. * Returns: The "p" parameter of the distribution.
  96. */
  97. RealType p() const { return _p; }
  98. /** Returns the smallest value that the distribution can produce. */
  99. bool min BOOST_PREVENT_MACRO_SUBSTITUTION () const
  100. { return false; }
  101. /** Returns the largest value that the distribution can produce. */
  102. bool max BOOST_PREVENT_MACRO_SUBSTITUTION () const
  103. { return true; }
  104. /** Returns the parameters of the distribution. */
  105. param_type param() const { return param_type(_p); }
  106. /** Sets the parameters of the distribution. */
  107. void param(const param_type& parm) { _p = parm.p(); }
  108. /**
  109. * Effects: Subsequent uses of the distribution do not depend
  110. * on values produced by any engine prior to invoking reset.
  111. */
  112. void reset() { }
  113. /**
  114. * Returns: a random variate distributed according to the
  115. * \bernoulli_distribution.
  116. */
  117. template<class Engine>
  118. bool operator()(Engine& eng) const
  119. {
  120. if(_p == RealType(0))
  121. return false;
  122. else
  123. return RealType(eng() - (eng.min)()) <= _p * RealType((eng.max)()-(eng.min)());
  124. }
  125. /**
  126. * Returns: a random variate distributed according to the
  127. * \bernoulli_distribution with parameters specified by param.
  128. */
  129. template<class Engine>
  130. bool operator()(Engine& eng, const param_type& parm) const
  131. {
  132. return bernoulli_distribution(parm)(eng);
  133. }
  134. /**
  135. * Writes the parameters of the distribution to a @c std::ostream.
  136. */
  137. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, bernoulli_distribution, bd)
  138. {
  139. os << bd._p;
  140. return os;
  141. }
  142. /**
  143. * Reads the parameters of the distribution from a @c std::istream.
  144. */
  145. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, bernoulli_distribution, bd)
  146. {
  147. is >> bd._p;
  148. return is;
  149. }
  150. /**
  151. * Returns true iff the two distributions will produce identical
  152. * sequences of values given equal generators.
  153. */
  154. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(bernoulli_distribution, lhs, rhs)
  155. { return lhs._p == rhs._p; }
  156. /**
  157. * Returns true iff the two distributions will produce different
  158. * sequences of values given equal generators.
  159. */
  160. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(bernoulli_distribution)
  161. private:
  162. RealType _p;
  163. };
  164. } // namespace random
  165. using random::bernoulli_distribution;
  166. } // namespace boost
  167. #endif // BOOST_RANDOM_BERNOULLI_DISTRIBUTION_HPP