| 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,v 1.17 2005/01/28 15:04:17 dgregor Exp $ | 
|---|
| 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 |  | 
|---|
| 26 | namespace boost { | 
|---|
| 27 |  | 
|---|
| 28 | // uniform distribution on a real range | 
|---|
| 29 | template<class RealType = double> | 
|---|
| 30 | class uniform_real | 
|---|
| 31 | { | 
|---|
| 32 | public: | 
|---|
| 33 |   typedef RealType input_type; | 
|---|
| 34 |   typedef RealType result_type; | 
|---|
| 35 |  | 
|---|
| 36 |   explicit uniform_real(RealType min = RealType(0), | 
|---|
| 37 |                         RealType max = RealType(1)) | 
|---|
| 38 |     : _min(min), _max(max) | 
|---|
| 39 |   { | 
|---|
| 40 | #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS | 
|---|
| 41 |     BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer); | 
|---|
| 42 | #endif | 
|---|
| 43 |     assert(min < max); | 
|---|
| 44 |   } | 
|---|
| 45 |  | 
|---|
| 46 |   // compiler-generated copy ctor and assignment operator are fine | 
|---|
| 47 |  | 
|---|
| 48 |   result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; } | 
|---|
| 49 |   result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; } | 
|---|
| 50 |   void reset() { } | 
|---|
| 51 |  | 
|---|
| 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; | 
|---|
| 57 |   } | 
|---|
| 58 |  | 
|---|
| 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) | 
|---|
| 63 |   { | 
|---|
| 64 |     os << ud._min << " " << ud._max; | 
|---|
| 65 |     return os; | 
|---|
| 66 |   } | 
|---|
| 67 |  | 
|---|
| 68 |   template<class CharT, class Traits> | 
|---|
| 69 |   friend std::basic_istream<CharT,Traits>& | 
|---|
| 70 |   operator>>(std::basic_istream<CharT,Traits>& is, uniform_real& ud) | 
|---|
| 71 |   { | 
|---|
| 72 |     is >> std::ws >> ud._min >> std::ws >> ud._max; | 
|---|
| 73 |     return is; | 
|---|
| 74 |   } | 
|---|
| 75 | #endif | 
|---|
| 76 |  | 
|---|
| 77 | private: | 
|---|
| 78 |   RealType _min, _max; | 
|---|
| 79 | }; | 
|---|
| 80 |  | 
|---|
| 81 | } // namespace boost | 
|---|
| 82 |  | 
|---|
| 83 | #endif // BOOST_RANDOM_UNIFORM_REAL_HPP | 
|---|