|
| 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 | + |
| 15 | +#ifndef BOOST_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP |
| 16 | +#define BOOST_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP |
| 17 | + |
| 18 | +#include <iosfwd> |
| 19 | +#include <ios> |
| 20 | +#include <istream> |
| 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 | + |
| 28 | +namespace boost { |
| 29 | +namespace random { |
| 30 | +namespace detail { |
| 31 | + |
| 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> */) |
| 36 | +{ |
| 37 | + for(;;) { |
| 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; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 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> */) |
| 53 | +{ |
| 54 | + for(;;) { |
| 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; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +template<class Engine, class T> |
| 67 | +inline T generate_uniform_real(Engine& eng, T min_value, T max_value) |
| 68 | +{ |
| 69 | + typedef typename Engine::result_type base_result; |
| 70 | + return generate_uniform_real(eng, min_value, max_value, |
| 71 | + boost::is_integral<base_result>()); |
| 72 | +} |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 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). |
| 80 | + */ |
| 81 | +template<class RealType = double> |
| 82 | +class uniform_real_distribution |
| 83 | +{ |
| 84 | +public: |
| 85 | + typedef RealType input_type; |
| 86 | + typedef RealType result_type; |
| 87 | + |
| 88 | + class param_type |
| 89 | + { |
| 90 | + public: |
| 91 | + |
| 92 | + typedef uniform_real_distribution distribution_type; |
| 93 | + |
| 94 | + /** |
| 95 | + * Constructs the parameters of a uniform_real_distribution. |
| 96 | + * |
| 97 | + * Requires min <= max |
| 98 | + */ |
| 99 | + explicit param_type(RealType min_arg = RealType(0.0), |
| 100 | + RealType max_arg = RealType(1.0)) |
| 101 | + : _min(min_arg), _max(max_arg) |
| 102 | + { |
| 103 | + BOOST_ASSERT(_min <= _max); |
| 104 | + } |
| 105 | + |
| 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; } |
| 110 | + |
| 111 | + /** Writes the parameters to a @c std::ostream. */ |
| 112 | + BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm) |
| 113 | + { |
| 114 | + os << parm._min << " " << parm._max; |
| 115 | + return os; |
| 116 | + } |
| 117 | + |
| 118 | + /** Reads the parameters from a @c std::istream. */ |
| 119 | + BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm) |
| 120 | + { |
| 121 | + RealType min_in, max_in; |
| 122 | + if(is >> min_in >> std::ws >> max_in) { |
| 123 | + if(min_in <= max_in) { |
| 124 | + parm._min = min_in; |
| 125 | + parm._max = max_in; |
| 126 | + } else { |
| 127 | + is.setstate(std::ios_base::failbit); |
| 128 | + } |
| 129 | + } |
| 130 | + return is; |
| 131 | + } |
| 132 | + |
| 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; } |
| 136 | + |
| 137 | + /** Returns true if the two sets of parameters are different. */ |
| 138 | + BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type) |
| 139 | + |
| 140 | + private: |
| 141 | + |
| 142 | + RealType _min; |
| 143 | + RealType _max; |
| 144 | + }; |
| 145 | + |
| 146 | + /** |
| 147 | + * Constructs a uniform_real_distribution. @c min and @c max are |
| 148 | + * the parameters of the distribution. |
| 149 | + * |
| 150 | + * Requires: min <= max |
| 151 | + */ |
| 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) |
| 156 | + { |
| 157 | + BOOST_ASSERT(min_arg <= max_arg); |
| 158 | + } |
| 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()) {} |
| 162 | + |
| 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; } |
| 167 | + |
| 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; } |
| 172 | + |
| 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) |
| 177 | + { |
| 178 | + _min = parm.a(); |
| 179 | + _max = parm.b(); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Effects: Subsequent uses of the distribution do not depend |
| 184 | + * on values produced by any engine prior to invoking reset. |
| 185 | + */ |
| 186 | + void reset() { } |
| 187 | + |
| 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); } |
| 192 | + |
| 193 | + /** |
| 194 | + * Returns a value uniformly distributed in the range |
| 195 | + * [param.a(), param.b()). |
| 196 | + */ |
| 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()); } |
| 200 | + |
| 201 | + /** Writes the distribution to a @c std::ostream. */ |
| 202 | + BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, uniform_real_distribution, ud) |
| 203 | + { |
| 204 | + os << ud.param(); |
| 205 | + return os; |
| 206 | + } |
| 207 | + |
| 208 | + /** Reads the distribution from a @c std::istream. */ |
| 209 | + BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, uniform_real_distribution, ud) |
| 210 | + { |
| 211 | + param_type parm; |
| 212 | + if(is >> parm) { |
| 213 | + ud.param(parm); |
| 214 | + } |
| 215 | + return is; |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Returns true if the two distributions will produce identical sequences |
| 220 | + * of values given equal generators. |
| 221 | + */ |
| 222 | + BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(uniform_real_distribution, lhs, rhs) |
| 223 | + { return lhs._min == rhs._min && lhs._max == rhs._max; } |
| 224 | + |
| 225 | + /** |
| 226 | + * Returns true if the two distributions may produce different sequences |
| 227 | + * of values given equal generators. |
| 228 | + */ |
| 229 | + BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(uniform_real_distribution) |
| 230 | + |
| 231 | +private: |
| 232 | + RealType _min; |
| 233 | + RealType _max; |
| 234 | +}; |
| 235 | + |
| 236 | +} // namespace random |
| 237 | +} // namespace boost |
| 238 | + |
| 239 | +#endif // BOOST_RANDOM_UNIFORM_INT_HPP |
0 commit comments