1 /* boost random/uniform_01.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.
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_UNIFORM_01_HPP
17 #define BOOST_RANDOM_UNIFORM_01_HPP
20 #include <boost/config.hpp>
21 #include <boost/limits.hpp>
22 #include <boost/static_assert.hpp>
23 #include <boost/random/detail/config.hpp>
24 #include <boost/random/detail/ptr_helper.hpp>
26 #include <boost/random/detail/disable_warnings.hpp>
31 #ifdef BOOST_RANDOM_DOXYGEN
34 * The distribution function uniform_01 models a \random_distribution.
35 * On each invocation, it returns a random floating-point value
36 * uniformly distributed in the range [0..1).
38 * The template parameter RealType shall denote a float-like value type
39 * with support for binary operators +, -, and /.
41 * Note: The current implementation is buggy, because it may not fill
42 * all of the mantissa with random bits. I'm unsure how to fill a
43 * (to-be-invented) @c boost::bigfloat class with random bits efficiently.
44 * It's probably time for a traits class.
46 template<class RealType = double>
50 typedef RealType input_type;
51 typedef RealType result_type;
52 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const;
53 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const;
56 template<class Engine>
57 result_type operator()(Engine& eng);
59 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
60 template<class CharT, class Traits>
61 friend std::basic_ostream<CharT,Traits>&
62 operator<<(std::basic_ostream<CharT,Traits>& os, const new_uniform_01&)
67 template<class CharT, class Traits>
68 friend std::basic_istream<CharT,Traits>&
69 operator>>(std::basic_istream<CharT,Traits>& is, new_uniform_01&)
80 template<class RealType>
84 typedef RealType input_type;
85 typedef RealType result_type;
86 // compiler-generated copy ctor and copy assignment are fine
87 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }
88 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }
91 template<class Engine>
92 result_type operator()(Engine& eng) {
94 typedef typename Engine::result_type base_result;
95 result_type factor = result_type(1) /
96 (result_type(base_result((eng.max)()-(eng.min)())) +
97 result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0));
98 result_type result = result_type(base_result(eng() - (eng.min)())) * factor;
99 if (result < result_type(1))
104 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
105 template<class CharT, class Traits>
106 friend std::basic_ostream<CharT,Traits>&
107 operator<<(std::basic_ostream<CharT,Traits>& os, const new_uniform_01&)
112 template<class CharT, class Traits>
113 friend std::basic_istream<CharT,Traits>&
114 operator>>(std::basic_istream<CharT,Traits>& is, new_uniform_01&)
121 template<class UniformRandomNumberGenerator, class RealType>
122 class backward_compatible_uniform_01
124 typedef boost::random::detail::ptr_helper<UniformRandomNumberGenerator> traits;
126 typedef UniformRandomNumberGenerator base_type;
127 typedef RealType result_type;
129 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
131 #if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS)
132 BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
135 explicit backward_compatible_uniform_01(typename traits::rvalue_type rng)
137 _factor(result_type(1) /
138 (result_type((base().max)()-(base().min)()) +
139 result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0)))
142 // compiler-generated copy ctor and copy assignment are fine
144 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }
145 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }
146 typename traits::value_type& base() { return traits::ref(_rng); }
147 const typename traits::value_type& base() const { return traits::ref(_rng); }
150 result_type operator()() {
152 result_type result = result_type(base()() - (base().min)()) * _factor;
153 if (result < result_type(1))
158 #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
159 template<class CharT, class Traits>
160 friend std::basic_ostream<CharT,Traits>&
161 operator<<(std::basic_ostream<CharT,Traits>& os, const backward_compatible_uniform_01& u)
167 template<class CharT, class Traits>
168 friend std::basic_istream<CharT,Traits>&
169 operator>>(std::basic_istream<CharT,Traits>& is, backward_compatible_uniform_01& u)
177 typedef typename traits::value_type::result_type base_result;
178 UniformRandomNumberGenerator _rng;
182 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
183 // A definition is required even for integral static constants
184 template<class UniformRandomNumberGenerator, class RealType>
185 const bool backward_compatible_uniform_01<UniformRandomNumberGenerator, RealType>::has_fixed_range;
188 template<class UniformRandomNumberGenerator, bool is_number = std::numeric_limits<UniformRandomNumberGenerator>::is_specialized>
189 struct select_uniform_01
191 template<class RealType>
194 typedef backward_compatible_uniform_01<UniformRandomNumberGenerator, RealType> type;
199 struct select_uniform_01<Num, true>
201 template<class RealType>
204 typedef new_uniform_01<Num> type;
210 // Because it is so commonly used: uniform distribution on the real [0..1)
211 // range. This allows for specializations to avoid a costly int -> float
212 // conversion plus float multiplication
213 template<class UniformRandomNumberGenerator = double, class RealType = double>
215 : public detail::select_uniform_01<UniformRandomNumberGenerator>::BOOST_NESTED_TEMPLATE apply<RealType>::type
217 typedef typename detail::select_uniform_01<UniformRandomNumberGenerator>::BOOST_NESTED_TEMPLATE apply<RealType>::type impl_type;
218 typedef boost::random::detail::ptr_helper<UniformRandomNumberGenerator> traits;
223 explicit uniform_01(typename traits::rvalue_type rng)
228 #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
229 template<class CharT, class Traits>
230 friend std::basic_ostream<CharT,Traits>&
231 operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_01& u)
233 os << static_cast<const impl_type&>(u);
237 template<class CharT, class Traits>
238 friend std::basic_istream<CharT,Traits>&
239 operator>>(std::basic_istream<CharT,Traits>& is, uniform_01& u)
241 is >> static_cast<impl_type&>(u);
249 } // namespace random
251 using random::uniform_01;
255 #include <boost/random/detail/enable_warnings.hpp>
257 #endif // BOOST_RANDOM_UNIFORM_01_HPP