1 /* boost random/uniform_01.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_01.hpp 58649 2010-01-02 21:23:17Z steven_watanabe $
\r
13 * 2001-02-18 moved to individual header files
\r
16 #ifndef BOOST_RANDOM_UNIFORM_01_HPP
\r
17 #define BOOST_RANDOM_UNIFORM_01_HPP
\r
20 #include <boost/config.hpp>
\r
21 #include <boost/limits.hpp>
\r
22 #include <boost/static_assert.hpp>
\r
23 #include <boost/random/detail/config.hpp>
\r
24 #include <boost/random/detail/pass_through_engine.hpp>
\r
26 #include <boost/random/detail/disable_warnings.hpp>
\r
32 template<class RealType>
\r
33 class new_uniform_01
\r
36 typedef RealType input_type;
\r
37 typedef RealType result_type;
\r
38 // compiler-generated copy ctor and copy assignment are fine
\r
39 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }
\r
40 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }
\r
43 template<class Engine>
\r
44 result_type operator()(Engine& eng) {
\r
46 typedef typename Engine::result_type base_result;
\r
47 result_type factor = result_type(1) /
\r
48 (result_type((eng.max)()-(eng.min)()) +
\r
49 result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0));
\r
50 result_type result = result_type(eng() - (eng.min)()) * factor;
\r
51 if (result < result_type(1))
\r
56 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
\r
57 template<class CharT, class Traits>
\r
58 friend std::basic_ostream<CharT,Traits>&
\r
59 operator<<(std::basic_ostream<CharT,Traits>& os, const new_uniform_01&)
\r
64 template<class CharT, class Traits>
\r
65 friend std::basic_istream<CharT,Traits>&
\r
66 operator>>(std::basic_istream<CharT,Traits>& is, new_uniform_01&)
\r
73 template<class UniformRandomNumberGenerator, class RealType>
\r
74 class backward_compatible_uniform_01
\r
76 typedef boost::random::detail::ptr_helper<UniformRandomNumberGenerator> traits;
\r
77 typedef boost::random::detail::pass_through_engine<UniformRandomNumberGenerator> internal_engine_type;
\r
79 typedef UniformRandomNumberGenerator base_type;
\r
80 typedef RealType result_type;
\r
82 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
\r
84 #if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
\r
85 BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
\r
88 explicit backward_compatible_uniform_01(typename traits::rvalue_type rng)
\r
90 _factor(result_type(1) /
\r
91 (result_type((_rng.max)()-(_rng.min)()) +
\r
92 result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0)))
\r
95 // compiler-generated copy ctor and copy assignment are fine
\r
97 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }
\r
98 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }
\r
99 typename traits::value_type& base() { return _rng.base(); }
\r
100 const typename traits::value_type& base() const { return _rng.base(); }
\r
103 result_type operator()() {
\r
105 result_type result = result_type(_rng() - (_rng.min)()) * _factor;
\r
106 if (result < result_type(1))
\r
111 #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
\r
112 template<class CharT, class Traits>
\r
113 friend std::basic_ostream<CharT,Traits>&
\r
114 operator<<(std::basic_ostream<CharT,Traits>& os, const backward_compatible_uniform_01& u)
\r
120 template<class CharT, class Traits>
\r
121 friend std::basic_istream<CharT,Traits>&
\r
122 operator>>(std::basic_istream<CharT,Traits>& is, backward_compatible_uniform_01& u)
\r
130 typedef typename internal_engine_type::result_type base_result;
\r
131 internal_engine_type _rng;
\r
132 result_type _factor;
\r
135 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
\r
136 // A definition is required even for integral static constants
\r
137 template<class UniformRandomNumberGenerator, class RealType>
\r
138 const bool backward_compatible_uniform_01<UniformRandomNumberGenerator, RealType>::has_fixed_range;
\r
141 template<class UniformRandomNumberGenerator>
\r
142 struct select_uniform_01
\r
144 template<class RealType>
\r
147 typedef backward_compatible_uniform_01<UniformRandomNumberGenerator, RealType> type;
\r
152 struct select_uniform_01<float>
\r
154 template<class RealType>
\r
157 typedef new_uniform_01<float> type;
\r
162 struct select_uniform_01<double>
\r
164 template<class RealType>
\r
167 typedef new_uniform_01<double> type;
\r
172 struct select_uniform_01<long double>
\r
174 template<class RealType>
\r
177 typedef new_uniform_01<long double> type;
\r
183 // Because it is so commonly used: uniform distribution on the real [0..1)
\r
184 // range. This allows for specializations to avoid a costly int -> float
\r
185 // conversion plus float multiplication
\r
186 template<class UniformRandomNumberGenerator = double, class RealType = double>
\r
188 : public detail::select_uniform_01<UniformRandomNumberGenerator>::BOOST_NESTED_TEMPLATE apply<RealType>::type
\r
190 typedef typename detail::select_uniform_01<UniformRandomNumberGenerator>::BOOST_NESTED_TEMPLATE apply<RealType>::type impl_type;
\r
191 typedef boost::random::detail::ptr_helper<UniformRandomNumberGenerator> traits;
\r
196 explicit uniform_01(typename traits::rvalue_type rng)
\r
201 #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
\r
202 template<class CharT, class Traits>
\r
203 friend std::basic_ostream<CharT,Traits>&
\r
204 operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_01& u)
\r
206 os << static_cast<const impl_type&>(u);
\r
210 template<class CharT, class Traits>
\r
211 friend std::basic_istream<CharT,Traits>&
\r
212 operator>>(std::basic_istream<CharT,Traits>& is, uniform_01& u)
\r
214 is >> static_cast<impl_type&>(u);
\r
220 } // namespace boost
\r
222 #include <boost/random/detail/enable_warnings.hpp>
\r
224 #endif // BOOST_RANDOM_UNIFORM_01_HPP
\r