]> git.donarmstrong.com Git - rsem.git/blob - boost/random/uniform_real.hpp
RSEM Source Codes
[rsem.git] / boost / random / uniform_real.hpp
1 /* boost random/uniform_real.hpp header file
2  *
3  * Copyright Jens Maurer 2000-2001
4  * Distributed under the Boost Software License, Version 1.0. (See
5  * accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  * See http://www.boost.org for most recent version including documentation.
9  *
10  * $Id: uniform_real.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
11  *
12  * Revision history
13  *  2001-04-08  added min<max assertion (N. Becker)
14  *  2001-02-18  moved to individual header files
15  */
16
17 #ifndef BOOST_RANDOM_UNIFORM_REAL_HPP
18 #define BOOST_RANDOM_UNIFORM_REAL_HPP
19
20 #include <cassert>
21 #include <iostream>
22 #include <boost/config.hpp>
23 #include <boost/limits.hpp>
24 #include <boost/static_assert.hpp>
25 #include <boost/random/detail/config.hpp>
26
27 namespace boost {
28
29 /**
30  * The distribution function uniform_real models a random distribution.
31  * On each invocation, it returns a random floating-point value uniformly
32  * distributed in the range [min..max). The value is computed using
33  * std::numeric_limits<RealType>::digits random binary digits, i.e.
34  * the mantissa of the floating-point value is completely filled with
35  * random bits.
36  *
37  * Note: The current implementation is buggy, because it may not fill
38  * all of the mantissa with random bits.
39  */
40 template<class RealType = double>
41 class uniform_real
42 {
43 public:
44   typedef RealType input_type;
45   typedef RealType result_type;
46
47   /**
48    * Constructs a uniform_real object. @c min and @c max are the
49    * parameters of the distribution.
50    *
51    * Requires: min <= max
52    */
53   explicit uniform_real(RealType min_arg = RealType(0),
54                         RealType max_arg = RealType(1))
55     : _min(min_arg), _max(max_arg)
56   {
57 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
58     BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
59 #endif
60     assert(min_arg <= max_arg);
61   }
62
63   // compiler-generated copy ctor and assignment operator are fine
64
65   /**
66    * Returns: The "min" parameter of the distribution
67    */
68   result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; }
69   /**
70    * Returns: The "max" parameter of the distribution
71    */
72   result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; }
73   void reset() { }
74
75   template<class Engine>
76   result_type operator()(Engine& eng) {
77     result_type numerator = static_cast<result_type>(eng() - eng.min BOOST_PREVENT_MACRO_SUBSTITUTION());
78     result_type divisor = static_cast<result_type>(eng.max BOOST_PREVENT_MACRO_SUBSTITUTION() - eng.min BOOST_PREVENT_MACRO_SUBSTITUTION());
79     assert(divisor > 0);
80     assert(numerator >= 0 && numerator <= divisor);
81     return numerator / divisor * (_max - _min) + _min;
82   }
83
84 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
85   template<class CharT, class Traits>
86   friend std::basic_ostream<CharT,Traits>&
87   operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_real& ud)
88   {
89     os << ud._min << " " << ud._max;
90     return os;
91   }
92
93   template<class CharT, class Traits>
94   friend std::basic_istream<CharT,Traits>&
95   operator>>(std::basic_istream<CharT,Traits>& is, uniform_real& ud)
96   {
97     is >> std::ws >> ud._min >> std::ws >> ud._max;
98     return is;
99   }
100 #endif
101
102 private:
103   RealType _min, _max;
104 };
105
106 } // namespace boost
107
108 #endif // BOOST_RANDOM_UNIFORM_REAL_HPP