uniform_real_distribution.hpp 7.5 KB

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