uniform_real_distribution.hpp 7.5 KB

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