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.
11 * $Id: uniform_real_distribution.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
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>
32 template<class Engine, class T>
33 T generate_uniform_real(
34 Engine& eng, T min_value, T max_value,
35 boost::mpl::false_ /** is_integral<Engine::result_type> */)
38 typedef T result_type;
39 typedef typename Engine::result_type base_result;
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 typedef typename Engine::result_type base_result;
70 return generate_uniform_real(eng, min_value, max_value,
71 boost::is_integral<base_result>());
77 * The class template uniform_real_distribution models a \random_distribution.
78 * On each invocation, it returns a random floating-point value uniformly
79 * distributed in the range [min..max).
81 template<class RealType = double>
82 class uniform_real_distribution
85 typedef RealType input_type;
86 typedef RealType result_type;
92 typedef uniform_real_distribution distribution_type;
95 * Constructs the parameters of a uniform_real_distribution.
99 explicit param_type(RealType min_arg = RealType(0.0),
100 RealType max_arg = RealType(1.0))
101 : _min(min_arg), _max(max_arg)
103 BOOST_ASSERT(_min <= _max);
106 /** Returns the minimum value of the distribution. */
107 RealType a() const { return _min; }
108 /** Returns the maximum value of the distribution. */
109 RealType b() const { return _max; }
111 /** Writes the parameters to a @c std::ostream. */
112 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
114 os << parm._min << " " << parm._max;
118 /** Reads the parameters from a @c std::istream. */
119 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
121 RealType min_in, max_in;
122 if(is >> min_in >> std::ws >> max_in) {
123 if(min_in <= max_in) {
127 is.setstate(std::ios_base::failbit);
133 /** Returns true if the two sets of parameters are equal. */
134 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
135 { return lhs._min == rhs._min && lhs._max == rhs._max; }
137 /** Returns true if the two sets of parameters are different. */
138 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
147 * Constructs a uniform_real_distribution. @c min and @c max are
148 * the parameters of the distribution.
150 * Requires: min <= max
152 explicit uniform_real_distribution(
153 RealType min_arg = RealType(0.0),
154 RealType max_arg = RealType(1.0))
155 : _min(min_arg), _max(max_arg)
157 BOOST_ASSERT(min_arg <= max_arg);
159 /** Constructs a uniform_real_distribution from its parameters. */
160 explicit uniform_real_distribution(const param_type& parm)
161 : _min(parm.a()), _max(parm.b()) {}
163 /** Returns the minimum value of the distribution */
164 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; }
165 /** Returns the maximum value of the distribution */
166 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; }
168 /** Returns the minimum value of the distribution */
169 RealType a() const { return _min; }
170 /** Returns the maximum value of the distribution */
171 RealType b() const { return _max; }
173 /** Returns the parameters of the distribution. */
174 param_type param() const { return param_type(_min, _max); }
175 /** Sets the parameters of the distribution. */
176 void param(const param_type& parm)
183 * Effects: Subsequent uses of the distribution do not depend
184 * on values produced by any engine prior to invoking reset.
188 /** Returns a value uniformly distributed in the range [min, max). */
189 template<class Engine>
190 result_type operator()(Engine& eng) const
191 { return detail::generate_uniform_real(eng, _min, _max); }
194 * Returns a value uniformly distributed in the range
195 * [param.a(), param.b()).
197 template<class Engine>
198 result_type operator()(Engine& eng, const param_type& parm) const
199 { return detail::generate_uniform_real(eng, parm.a(), parm.b()); }
201 /** Writes the distribution to a @c std::ostream. */
202 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, uniform_real_distribution, ud)
208 /** Reads the distribution from a @c std::istream. */
209 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, uniform_real_distribution, ud)
219 * Returns true if the two distributions will produce identical sequences
220 * of values given equal generators.
222 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(uniform_real_distribution, lhs, rhs)
223 { return lhs._min == rhs._min && lhs._max == rhs._max; }
226 * Returns true if the two distributions may produce different sequences
227 * of values given equal generators.
229 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(uniform_real_distribution)
236 } // namespace random
239 #endif // BOOST_RANDOM_UNIFORM_INT_HPP