1 /* boost random/uniform_real_distribution.hpp header file
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)
9 * See http://www.boost.org for most recent version including documentation.
15 #ifndef BOOST_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP
16 #define BOOST_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP
21 #include <boost/assert.hpp>
22 #include <boost/config.hpp>
23 #include <boost/random/detail/config.hpp>
24 #include <boost/random/detail/operators.hpp>
25 #include <boost/random/detail/signed_unsigned_tools.hpp>
26 #include <boost/type_traits/is_integral.hpp>
27 #include <boost/mpl/bool.hpp>
33 template<class Engine, class T>
34 T generate_uniform_real(
35 Engine& eng, T min_value, T max_value,
36 boost::mpl::false_ /** is_integral<Engine::result_type> */)
39 typedef T result_type;
40 result_type numerator = static_cast<T>(eng() - (eng.min)());
41 result_type divisor = static_cast<T>((eng.max)() - (eng.min)());
42 BOOST_ASSERT(divisor > 0);
43 BOOST_ASSERT(numerator >= 0 && numerator <= divisor);
44 T result = numerator / divisor * (max_value - min_value) + min_value;
45 if(result < max_value) return result;
49 template<class Engine, class T>
50 T generate_uniform_real(
51 Engine& eng, T min_value, T max_value,
52 boost::mpl::true_ /** is_integral<Engine::result_type> */)
55 typedef T result_type;
56 typedef typename Engine::result_type base_result;
57 result_type numerator = static_cast<T>(subtract<base_result>()(eng(), (eng.min)()));
58 result_type divisor = static_cast<T>(subtract<base_result>()((eng.max)(), (eng.min)())) + 1;
59 BOOST_ASSERT(divisor > 0);
60 BOOST_ASSERT(numerator >= 0 && numerator <= divisor);
61 T result = numerator / divisor * (max_value - min_value) + min_value;
62 if(result < max_value) return result;
66 template<class Engine, class T>
67 inline T generate_uniform_real(Engine& eng, T min_value, T max_value)
69 if(max_value / 2 - min_value / 2 > (std::numeric_limits<T>::max)() / 2)
70 return 2 * generate_uniform_real(eng, T(min_value / 2), T(max_value / 2));
71 typedef typename Engine::result_type base_result;
72 return generate_uniform_real(eng, min_value, max_value,
73 boost::is_integral<base_result>());
79 * The class template uniform_real_distribution models a \random_distribution.
80 * On each invocation, it returns a random floating-point value uniformly
81 * distributed in the range [min..max).
83 template<class RealType = double>
84 class uniform_real_distribution
87 typedef RealType input_type;
88 typedef RealType result_type;
94 typedef uniform_real_distribution distribution_type;
97 * Constructs the parameters of a uniform_real_distribution.
101 explicit param_type(RealType min_arg = RealType(0.0),
102 RealType max_arg = RealType(1.0))
103 : _min(min_arg), _max(max_arg)
105 BOOST_ASSERT(_min < _max);
108 /** Returns the minimum value of the distribution. */
109 RealType a() const { return _min; }
110 /** Returns the maximum value of the distribution. */
111 RealType b() const { return _max; }
113 /** Writes the parameters to a @c std::ostream. */
114 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
116 os << parm._min << " " << parm._max;
120 /** Reads the parameters from a @c std::istream. */
121 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
123 RealType min_in, max_in;
124 if(is >> min_in >> std::ws >> max_in) {
125 if(min_in <= max_in) {
129 is.setstate(std::ios_base::failbit);
135 /** Returns true if the two sets of parameters are equal. */
136 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
137 { return lhs._min == rhs._min && lhs._max == rhs._max; }
139 /** Returns true if the two sets of parameters are different. */
140 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
149 * Constructs a uniform_real_distribution. @c min and @c max are
150 * the parameters of the distribution.
152 * Requires: min <= max
154 explicit uniform_real_distribution(
155 RealType min_arg = RealType(0.0),
156 RealType max_arg = RealType(1.0))
157 : _min(min_arg), _max(max_arg)
159 BOOST_ASSERT(min_arg < max_arg);
161 /** Constructs a uniform_real_distribution from its parameters. */
162 explicit uniform_real_distribution(const param_type& parm)
163 : _min(parm.a()), _max(parm.b()) {}
165 /** Returns the minimum value of the distribution */
166 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; }
167 /** Returns the maximum value of the distribution */
168 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; }
170 /** Returns the minimum value of the distribution */
171 RealType a() const { return _min; }
172 /** Returns the maximum value of the distribution */
173 RealType b() const { return _max; }
175 /** Returns the parameters of the distribution. */
176 param_type param() const { return param_type(_min, _max); }
177 /** Sets the parameters of the distribution. */
178 void param(const param_type& parm)
185 * Effects: Subsequent uses of the distribution do not depend
186 * on values produced by any engine prior to invoking reset.
190 /** Returns a value uniformly distributed in the range [min, max). */
191 template<class Engine>
192 result_type operator()(Engine& eng) const
193 { return detail::generate_uniform_real(eng, _min, _max); }
196 * Returns a value uniformly distributed in the range
197 * [param.a(), param.b()).
199 template<class Engine>
200 result_type operator()(Engine& eng, const param_type& parm) const
201 { return detail::generate_uniform_real(eng, parm.a(), parm.b()); }
203 /** Writes the distribution to a @c std::ostream. */
204 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, uniform_real_distribution, ud)
210 /** Reads the distribution from a @c std::istream. */
211 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, uniform_real_distribution, ud)
221 * Returns true if the two distributions will produce identical sequences
222 * of values given equal generators.
224 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(uniform_real_distribution, lhs, rhs)
225 { return lhs._min == rhs._min && lhs._max == rhs._max; }
228 * Returns true if the two distributions may produce different sequences
229 * of values given equal generators.
231 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(uniform_real_distribution)
238 } // namespace random
241 #endif // BOOST_RANDOM_UNIFORM_INT_HPP