1 /* boost random/uniform_real.hpp header file
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)
8 * See http://www.boost.org for most recent version including documentation.
10 * $Id: uniform_real.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
13 * 2001-04-08 added min<max assertion (N. Becker)
14 * 2001-02-18 moved to individual header files
17 #ifndef BOOST_RANDOM_UNIFORM_REAL_HPP
18 #define BOOST_RANDOM_UNIFORM_REAL_HPP
22 #include <boost/config.hpp>
23 #include <boost/limits.hpp>
24 #include <boost/static_assert.hpp>
25 #include <boost/random/detail/config.hpp>
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
37 * Note: The current implementation is buggy, because it may not fill
38 * all of the mantissa with random bits.
40 template<class RealType = double>
44 typedef RealType input_type;
45 typedef RealType result_type;
48 * Constructs a uniform_real object. @c min and @c max are the
49 * parameters of the distribution.
51 * Requires: min <= max
53 explicit uniform_real(RealType min_arg = RealType(0),
54 RealType max_arg = RealType(1))
55 : _min(min_arg), _max(max_arg)
57 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
58 BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
60 assert(min_arg <= max_arg);
63 // compiler-generated copy ctor and assignment operator are fine
66 * Returns: The "min" parameter of the distribution
68 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; }
70 * Returns: The "max" parameter of the distribution
72 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; }
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());
80 assert(numerator >= 0 && numerator <= divisor);
81 return numerator / divisor * (_max - _min) + _min;
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)
89 os << ud._min << " " << ud._max;
93 template<class CharT, class Traits>
94 friend std::basic_istream<CharT,Traits>&
95 operator>>(std::basic_istream<CharT,Traits>& is, uniform_real& ud)
97 is >> std::ws >> ud._min >> std::ws >> ud._max;
108 #endif // BOOST_RANDOM_UNIFORM_REAL_HPP