]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
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 | #include <boost/mpl/bool.hpp> | |
28 | ||
29 | namespace boost { | |
30 | namespace random { | |
31 | namespace detail { | |
32 | ||
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> */) | |
37 | { | |
38 | for(;;) { | |
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; | |
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 | 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>()); | |
74 | } | |
75 | ||
76 | } | |
77 | ||
78 | /** | |
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). | |
82 | */ | |
83 | template<class RealType = double> | |
84 | class uniform_real_distribution | |
85 | { | |
86 | public: | |
87 | typedef RealType input_type; | |
88 | typedef RealType result_type; | |
89 | ||
90 | class param_type | |
91 | { | |
92 | public: | |
93 | ||
94 | typedef uniform_real_distribution distribution_type; | |
95 | ||
96 | /** | |
97 | * Constructs the parameters of a uniform_real_distribution. | |
98 | * | |
99 | * Requires min <= max | |
100 | */ | |
101 | explicit param_type(RealType min_arg = RealType(0.0), | |
102 | RealType max_arg = RealType(1.0)) | |
103 | : _min(min_arg), _max(max_arg) | |
104 | { | |
105 | BOOST_ASSERT(_min < _max); | |
106 | } | |
107 | ||
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; } | |
112 | ||
113 | /** Writes the parameters to a @c std::ostream. */ | |
114 | BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm) | |
115 | { | |
116 | os << parm._min << " " << parm._max; | |
117 | return os; | |
118 | } | |
119 | ||
120 | /** Reads the parameters from a @c std::istream. */ | |
121 | BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm) | |
122 | { | |
123 | RealType min_in, max_in; | |
124 | if(is >> min_in >> std::ws >> max_in) { | |
125 | if(min_in <= max_in) { | |
126 | parm._min = min_in; | |
127 | parm._max = max_in; | |
128 | } else { | |
129 | is.setstate(std::ios_base::failbit); | |
130 | } | |
131 | } | |
132 | return is; | |
133 | } | |
134 | ||
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; } | |
138 | ||
139 | /** Returns true if the two sets of parameters are different. */ | |
140 | BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type) | |
141 | ||
142 | private: | |
143 | ||
144 | RealType _min; | |
145 | RealType _max; | |
146 | }; | |
147 | ||
148 | /** | |
149 | * Constructs a uniform_real_distribution. @c min and @c max are | |
150 | * the parameters of the distribution. | |
151 | * | |
152 | * Requires: min <= max | |
153 | */ | |
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) | |
158 | { | |
159 | BOOST_ASSERT(min_arg < max_arg); | |
160 | } | |
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()) {} | |
164 | ||
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; } | |
169 | ||
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; } | |
174 | ||
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) | |
179 | { | |
180 | _min = parm.a(); | |
181 | _max = parm.b(); | |
182 | } | |
183 | ||
184 | /** | |
185 | * Effects: Subsequent uses of the distribution do not depend | |
186 | * on values produced by any engine prior to invoking reset. | |
187 | */ | |
188 | void reset() { } | |
189 | ||
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); } | |
194 | ||
195 | /** | |
196 | * Returns a value uniformly distributed in the range | |
197 | * [param.a(), param.b()). | |
198 | */ | |
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()); } | |
202 | ||
203 | /** Writes the distribution to a @c std::ostream. */ | |
204 | BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, uniform_real_distribution, ud) | |
205 | { | |
206 | os << ud.param(); | |
207 | return os; | |
208 | } | |
209 | ||
210 | /** Reads the distribution from a @c std::istream. */ | |
211 | BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, uniform_real_distribution, ud) | |
212 | { | |
213 | param_type parm; | |
214 | if(is >> parm) { | |
215 | ud.param(parm); | |
216 | } | |
217 | return is; | |
218 | } | |
219 | ||
220 | /** | |
221 | * Returns true if the two distributions will produce identical sequences | |
222 | * of values given equal generators. | |
223 | */ | |
224 | BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(uniform_real_distribution, lhs, rhs) | |
225 | { return lhs._min == rhs._min && lhs._max == rhs._max; } | |
226 | ||
227 | /** | |
228 | * Returns true if the two distributions may produce different sequences | |
229 | * of values given equal generators. | |
230 | */ | |
231 | BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(uniform_real_distribution) | |
232 | ||
233 | private: | |
234 | RealType _min; | |
235 | RealType _max; | |
236 | }; | |
237 | ||
238 | } // namespace random | |
239 | } // namespace boost | |
240 | ||
241 | #endif // BOOST_RANDOM_UNIFORM_INT_HPP |