1 /* boost random/uniform_real.hpp header file
\r
3 * Copyright Jens Maurer 2000-2001
\r
4 * Distributed under the Boost Software License, Version 1.0. (See
\r
5 * accompanying file LICENSE_1_0.txt or copy at
\r
6 * http://www.boost.org/LICENSE_1_0.txt)
\r
8 * See http://www.boost.org for most recent version including documentation.
\r
10 * $Id: uniform_real.hpp 56814 2009-10-14 04:54:01Z steven_watanabe $
\r
13 * 2001-04-08 added min<max assertion (N. Becker)
\r
14 * 2001-02-18 moved to individual header files
\r
17 #ifndef BOOST_RANDOM_UNIFORM_REAL_HPP
\r
18 #define BOOST_RANDOM_UNIFORM_REAL_HPP
\r
22 #include <boost/config.hpp>
\r
23 #include <boost/limits.hpp>
\r
24 #include <boost/static_assert.hpp>
\r
25 #include <boost/random/detail/config.hpp>
\r
29 // uniform distribution on a real range
\r
30 template<class RealType = double>
\r
34 typedef RealType input_type;
\r
35 typedef RealType result_type;
\r
37 explicit uniform_real(RealType min_arg = RealType(0),
\r
38 RealType max_arg = RealType(1))
\r
39 : _min(min_arg), _max(max_arg)
\r
41 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
\r
42 BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
\r
44 assert(min_arg <= max_arg);
\r
47 // compiler-generated copy ctor and assignment operator are fine
\r
49 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; }
\r
50 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; }
\r
53 template<class Engine>
\r
54 result_type operator()(Engine& eng) {
\r
55 result_type numerator = static_cast<result_type>(eng() - eng.min BOOST_PREVENT_MACRO_SUBSTITUTION());
\r
56 result_type divisor = static_cast<result_type>(eng.max BOOST_PREVENT_MACRO_SUBSTITUTION() - eng.min BOOST_PREVENT_MACRO_SUBSTITUTION());
\r
57 assert(divisor > 0);
\r
58 assert(numerator >= 0 && numerator <= divisor);
\r
59 return numerator / divisor * (_max - _min) + _min;
\r
62 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
\r
63 template<class CharT, class Traits>
\r
64 friend std::basic_ostream<CharT,Traits>&
\r
65 operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_real& ud)
\r
67 os << ud._min << " " << ud._max;
\r
71 template<class CharT, class Traits>
\r
72 friend std::basic_istream<CharT,Traits>&
\r
73 operator>>(std::basic_istream<CharT,Traits>& is, uniform_real& ud)
\r
75 is >> std::ws >> ud._min >> std::ws >> ud._max;
\r
81 RealType _min, _max;
\r
84 } // namespace boost
\r
86 #endif // BOOST_RANDOM_UNIFORM_REAL_HPP
\r