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,v 1.17 2005/01/28 15:04:17 dgregor Exp $
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>
28 // uniform distribution on a real range
29 template<class RealType = double>
33 typedef RealType input_type;
34 typedef RealType result_type;
36 explicit uniform_real(RealType min = RealType(0),
37 RealType max = RealType(1))
38 : _min(min), _max(max)
40 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
41 BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
46 // compiler-generated copy ctor and assignment operator are fine
48 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; }
49 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; }
52 template<class Engine>
53 result_type operator()(Engine& eng) {
54 return static_cast<result_type>(eng() - eng.min BOOST_PREVENT_MACRO_SUBSTITUTION())
55 / static_cast<result_type>(eng.max BOOST_PREVENT_MACRO_SUBSTITUTION() - eng.min BOOST_PREVENT_MACRO_SUBSTITUTION())
56 * (_max - _min) + _min;
59 #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
60 template<class CharT, class Traits>
61 friend std::basic_ostream<CharT,Traits>&
62 operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_real& ud)
64 os << ud._min << " " << ud._max;
68 template<class CharT, class Traits>
69 friend std::basic_istream<CharT,Traits>&
70 operator>>(std::basic_istream<CharT,Traits>& is, uniform_real& ud)
72 is >> std::ws >> ud._min >> std::ws >> ud._max;
83 #endif // BOOST_RANDOM_UNIFORM_REAL_HPP