uniform_real_distribution.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* boost random/uniform_real_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. */
  14. #ifndef BOOST_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP
  15. #define BOOST_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP
  16. #include <iosfwd>
  17. #include <ios>
  18. #include <istream>
  19. #include <boost/assert.hpp>
  20. #include <boost/config.hpp>
  21. #include <boost/random/detail/config.hpp>
  22. #include <boost/random/detail/operators.hpp>
  23. #include <boost/random/detail/signed_unsigned_tools.hpp>
  24. #include <boost/type_traits/is_integral.hpp>
  25. #include <boost/mpl/bool.hpp>
  26. namespace boost {
  27. namespace random {
  28. namespace detail {
  29. template<class Engine, class T>
  30. T generate_uniform_real(
  31. Engine& eng, T min_value, T max_value,
  32. boost::mpl::false_ /** is_integral<Engine::result_type> */)
  33. {
  34. for(;;) {
  35. typedef T result_type;
  36. result_type numerator = static_cast<T>(eng() - (eng.min)());
  37. result_type divisor = static_cast<T>((eng.max)() - (eng.min)());
  38. BOOST_ASSERT(divisor > 0);
  39. BOOST_ASSERT(numerator >= 0 && numerator <= divisor);
  40. T result = numerator / divisor * (max_value - min_value) + min_value;
  41. if(result < max_value) return result;
  42. }
  43. }
  44. template<class Engine, class T>
  45. T generate_uniform_real(
  46. Engine& eng, T min_value, T max_value,
  47. boost::mpl::true_ /** is_integral<Engine::result_type> */)
  48. {
  49. for(;;) {
  50. typedef T result_type;
  51. typedef typename Engine::result_type base_result;
  52. result_type numerator = static_cast<T>(subtract<base_result>()(eng(), (eng.min)()));
  53. result_type divisor = static_cast<T>(subtract<base_result>()((eng.max)(), (eng.min)())) + 1;
  54. BOOST_ASSERT(divisor > 0);
  55. BOOST_ASSERT(numerator >= 0 && numerator <= divisor);
  56. T result = numerator / divisor * (max_value - min_value) + min_value;
  57. if(result < max_value) return result;
  58. }
  59. }
  60. template<class Engine, class T>
  61. inline T generate_uniform_real(Engine& eng, T min_value, T max_value)
  62. {
  63. if(max_value / 2 - min_value / 2 > (std::numeric_limits<T>::max)() / 2)
  64. return 2 * generate_uniform_real(eng, T(min_value / 2), T(max_value / 2));
  65. typedef typename Engine::result_type base_result;
  66. return generate_uniform_real(eng, min_value, max_value,
  67. boost::is_integral<base_result>());
  68. }
  69. }
  70. /**
  71. * The class template uniform_real_distribution models a \random_distribution.
  72. * On each invocation, it returns a random floating-point value uniformly
  73. * distributed in the range [min..max).
  74. */
  75. template<class RealType = double>
  76. class uniform_real_distribution
  77. {
  78. public:
  79. typedef RealType input_type;
  80. typedef RealType result_type;
  81. class param_type
  82. {
  83. public:
  84. typedef uniform_real_distribution distribution_type;
  85. /**
  86. * Constructs the parameters of a uniform_real_distribution.
  87. *
  88. * Requires min <= max
  89. */
  90. explicit param_type(RealType min_arg = RealType(0.0),
  91. RealType max_arg = RealType(1.0))
  92. : _min(min_arg), _max(max_arg)
  93. {
  94. BOOST_ASSERT(_min < _max);
  95. }
  96. /** Returns the minimum value of the distribution. */
  97. RealType a() const { return _min; }
  98. /** Returns the maximum value of the distribution. */
  99. RealType b() const { return _max; }
  100. /** Writes the parameters to a @c std::ostream. */
  101. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
  102. {
  103. os << parm._min << " " << parm._max;
  104. return os;
  105. }
  106. /** Reads the parameters from a @c std::istream. */
  107. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
  108. {
  109. RealType min_in, max_in;
  110. if(is >> min_in >> std::ws >> max_in) {
  111. if(min_in <= max_in) {
  112. parm._min = min_in;
  113. parm._max = max_in;
  114. } else {
  115. is.setstate(std::ios_base::failbit);
  116. }
  117. }
  118. return is;
  119. }
  120. /** Returns true if the two sets of parameters are equal. */
  121. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
  122. { return lhs._min == rhs._min && lhs._max == rhs._max; }
  123. /** Returns true if the two sets of parameters are different. */
  124. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
  125. private:
  126. RealType _min;
  127. RealType _max;
  128. };
  129. /**
  130. * Constructs a uniform_real_distribution. @c min and @c max are
  131. * the parameters of the distribution.
  132. *
  133. * Requires: min <= max
  134. */
  135. explicit uniform_real_distribution(
  136. RealType min_arg = RealType(0.0),
  137. RealType max_arg = RealType(1.0))
  138. : _min(min_arg), _max(max_arg)
  139. {
  140. BOOST_ASSERT(min_arg < max_arg);
  141. }
  142. /** Constructs a uniform_real_distribution from its parameters. */
  143. explicit uniform_real_distribution(const param_type& parm)
  144. : _min(parm.a()), _max(parm.b()) {}
  145. /** Returns the minimum value of the distribution */
  146. RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; }
  147. /** Returns the maximum value of the distribution */
  148. RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; }
  149. /** Returns the minimum value of the distribution */
  150. RealType a() const { return _min; }
  151. /** Returns the maximum value of the distribution */
  152. RealType b() const { return _max; }
  153. /** Returns the parameters of the distribution. */
  154. param_type param() const { return param_type(_min, _max); }
  155. /** Sets the parameters of the distribution. */
  156. void param(const param_type& parm)
  157. {
  158. _min = parm.a();
  159. _max = parm.b();
  160. }
  161. /**
  162. * Effects: Subsequent uses of the distribution do not depend
  163. * on values produced by any engine prior to invoking reset.
  164. */
  165. void reset() { }
  166. /** Returns a value uniformly distributed in the range [min, max). */
  167. template<class Engine>
  168. result_type operator()(Engine& eng) const
  169. { return detail::generate_uniform_real(eng, _min, _max); }
  170. /**
  171. * Returns a value uniformly distributed in the range
  172. * [param.a(), param.b()).
  173. */
  174. template<class Engine>
  175. result_type operator()(Engine& eng, const param_type& parm) const
  176. { return detail::generate_uniform_real(eng, parm.a(), parm.b()); }
  177. /** Writes the distribution to a @c std::ostream. */
  178. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, uniform_real_distribution, ud)
  179. {
  180. os << ud.param();
  181. return os;
  182. }
  183. /** Reads the distribution from a @c std::istream. */
  184. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, uniform_real_distribution, ud)
  185. {
  186. param_type parm;
  187. if(is >> parm) {
  188. ud.param(parm);
  189. }
  190. return is;
  191. }
  192. /**
  193. * Returns true if the two distributions will produce identical sequences
  194. * of values given equal generators.
  195. */
  196. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(uniform_real_distribution, lhs, rhs)
  197. { return lhs._min == rhs._min && lhs._max == rhs._max; }
  198. /**
  199. * Returns true if the two distributions may produce different sequences
  200. * of values given equal generators.
  201. */
  202. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(uniform_real_distribution)
  203. private:
  204. RealType _min;
  205. RealType _max;
  206. };
  207. } // namespace random
  208. } // namespace boost
  209. #endif // BOOST_RANDOM_UNIFORM_INT_HPP