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 
28 namespace boost {
29 namespace random {
30 namespace detail {
31 
32 template<class Engine, class T>
generate_uniform_real(Engine & eng,T min_value,T max_value,boost::false_type)33 T generate_uniform_real(
34     Engine& eng, T min_value, T max_value,
35     boost::false_type  /** is_integral<Engine::result_type> */)
36 {
37     for(;;) {
38         typedef T result_type;
39         result_type numerator = static_cast<T>(eng() - (eng.min)());
40         result_type divisor = static_cast<T>((eng.max)() - (eng.min)());
41         BOOST_ASSERT(divisor > 0);
42         BOOST_ASSERT(numerator >= 0 && numerator <= divisor);
43         T result = numerator / divisor * (max_value - min_value) + min_value;
44         if(result < max_value) return result;
45     }
46 }
47 
48 template<class Engine, class T>
generate_uniform_real(Engine & eng,T min_value,T max_value,boost::true_type)49 T generate_uniform_real(
50     Engine& eng, T min_value, T max_value,
51     boost::true_type  /** is_integral<Engine::result_type> */)
52 {
53     for(;;) {
54         typedef T result_type;
55         typedef typename Engine::result_type base_result;
56         result_type numerator = static_cast<T>(subtract<base_result>()(eng(), (eng.min)()));
57         result_type divisor = static_cast<T>(subtract<base_result>()((eng.max)(), (eng.min)())) + 1;
58         BOOST_ASSERT(divisor > 0);
59         BOOST_ASSERT(numerator >= 0 && numerator <= divisor);
60         T result = numerator / divisor * (max_value - min_value) + min_value;
61         if(result < max_value) return result;
62     }
63 }
64 
65 template<class Engine, class T>
generate_uniform_real(Engine & eng,T min_value,T max_value)66 inline T generate_uniform_real(Engine& eng, T min_value, T max_value)
67 {
68     if(max_value / 2 - min_value / 2 > (std::numeric_limits<T>::max)() / 2)
69         return 2 * generate_uniform_real(eng, T(min_value / 2), T(max_value / 2));
70     typedef typename Engine::result_type base_result;
71     return generate_uniform_real(eng, min_value, max_value,
72         boost::is_integral<base_result>());
73 }
74 
75 }
76 
77 /**
78  * The class template uniform_real_distribution models a \random_distribution.
79  * On each invocation, it returns a random floating-point value uniformly
80  * distributed in the range [min..max).
81  */
82 template<class RealType = double>
83 class uniform_real_distribution
84 {
85 public:
86     typedef RealType input_type;
87     typedef RealType result_type;
88 
89     class param_type
90     {
91     public:
92 
93         typedef uniform_real_distribution distribution_type;
94 
95         /**
96          * Constructs the parameters of a uniform_real_distribution.
97          *
98          * Requires min <= max
99          */
param_type(RealType min_arg=RealType (0.0),RealType max_arg=RealType (1.0))100         explicit param_type(RealType min_arg = RealType(0.0),
101                             RealType max_arg = RealType(1.0))
102           : _min(min_arg), _max(max_arg)
103         {
104             BOOST_ASSERT(_min < _max);
105         }
106 
107         /** Returns the minimum value of the distribution. */
a() const108         RealType a() const { return _min; }
109         /** Returns the maximum value of the distribution. */
b() const110         RealType b() const { return _max; }
111 
112         /** Writes the parameters to a @c std::ostream. */
BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os,param_type,parm)113         BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
114         {
115             os << parm._min << " " << parm._max;
116             return os;
117         }
118 
119         /** Reads the parameters from a @c std::istream. */
BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is,param_type,parm)120         BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
121         {
122             RealType min_in, max_in;
123             if(is >> min_in >> std::ws >> max_in) {
124                 if(min_in <= max_in) {
125                     parm._min = min_in;
126                     parm._max = max_in;
127                 } else {
128                     is.setstate(std::ios_base::failbit);
129                 }
130             }
131             return is;
132         }
133 
134         /** Returns true if the two sets of parameters are equal. */
BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type,lhs,rhs)135         BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
136         { return lhs._min == rhs._min && lhs._max == rhs._max; }
137 
138         /** Returns true if the two sets of parameters are different. */
139         BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
140 
141     private:
142 
143         RealType _min;
144         RealType _max;
145     };
146 
147     /**
148      * Constructs a uniform_real_distribution. @c min and @c max are
149      * the parameters of the distribution.
150      *
151      * Requires: min <= max
152      */
uniform_real_distribution(RealType min_arg=RealType (0.0),RealType max_arg=RealType (1.0))153     explicit uniform_real_distribution(
154         RealType min_arg = RealType(0.0),
155         RealType max_arg = RealType(1.0))
156       : _min(min_arg), _max(max_arg)
157     {
158         BOOST_ASSERT(min_arg < max_arg);
159     }
160     /** Constructs a uniform_real_distribution from its parameters. */
uniform_real_distribution(const param_type & parm)161     explicit uniform_real_distribution(const param_type& parm)
162       : _min(parm.a()), _max(parm.b()) {}
163 
164     /**  Returns the minimum value of the distribution */
BOOST_PREVENT_MACRO_SUBSTITUTION() const165     RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; }
166     /**  Returns the maximum value of the distribution */
BOOST_PREVENT_MACRO_SUBSTITUTION() const167     RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; }
168 
169     /**  Returns the minimum value of the distribution */
a() const170     RealType a() const { return _min; }
171     /**  Returns the maximum value of the distribution */
b() const172     RealType b() const { return _max; }
173 
174     /** Returns the parameters of the distribution. */
param() const175     param_type param() const { return param_type(_min, _max); }
176     /** Sets the parameters of the distribution. */
param(const param_type & parm)177     void param(const param_type& parm)
178     {
179         _min = parm.a();
180         _max = parm.b();
181     }
182 
183     /**
184      * Effects: Subsequent uses of the distribution do not depend
185      * on values produced by any engine prior to invoking reset.
186      */
reset()187     void reset() { }
188 
189     /** Returns a value uniformly distributed in the range [min, max). */
190     template<class Engine>
operator ()(Engine & eng) const191     result_type operator()(Engine& eng) const
192     { return detail::generate_uniform_real(eng, _min, _max); }
193 
194     /**
195      * Returns a value uniformly distributed in the range
196      * [param.a(), param.b()).
197      */
198     template<class Engine>
operator ()(Engine & eng,const param_type & parm) const199     result_type operator()(Engine& eng, const param_type& parm) const
200     { return detail::generate_uniform_real(eng, parm.a(), parm.b()); }
201 
202     /** Writes the distribution to a @c std::ostream. */
BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os,uniform_real_distribution,ud)203     BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, uniform_real_distribution, ud)
204     {
205         os << ud.param();
206         return os;
207     }
208 
209     /** Reads the distribution from a @c std::istream. */
BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is,uniform_real_distribution,ud)210     BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, uniform_real_distribution, ud)
211     {
212         param_type parm;
213         if(is >> parm) {
214             ud.param(parm);
215         }
216         return is;
217     }
218 
219     /**
220      * Returns true if the two distributions will produce identical sequences
221      * of values given equal generators.
222      */
BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(uniform_real_distribution,lhs,rhs)223     BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(uniform_real_distribution, lhs, rhs)
224     { return lhs._min == rhs._min && lhs._max == rhs._max; }
225 
226     /**
227      * Returns true if the two distributions may produce different sequences
228      * of values given equal generators.
229      */
230     BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(uniform_real_distribution)
231 
232 private:
233     RealType _min;
234     RealType _max;
235 };
236 
237 } // namespace random
238 } // namespace boost
239 
240 #endif // BOOST_RANDOM_UNIFORM_INT_HPP
241