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.
10 * $Id: uniform_01.hpp,v 1.18 2004/07/27 03:43:32 dgregor Exp $
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>
26 // Because it is so commonly used: uniform distribution on the real [0..1)
27 // range. This allows for specializations to avoid a costly int -> float
28 // conversion plus float multiplication
29 template<class UniformRandomNumberGenerator, class RealType = double>
33 typedef UniformRandomNumberGenerator base_type;
34 typedef RealType result_type;
36 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
38 #if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
39 BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
42 explicit uniform_01(base_type rng)
44 _factor(result_type(1) /
45 (result_type((_rng.max)()-(_rng.min)()) +
46 result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0)))
49 // compiler-generated copy ctor and copy assignment are fine
51 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }
52 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }
53 base_type& base() { return _rng; }
54 const base_type& base() const { return _rng; }
57 result_type operator()() {
58 return result_type(_rng() - (_rng.min)()) * _factor;
61 #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
62 template<class CharT, class Traits>
63 friend std::basic_ostream<CharT,Traits>&
64 operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_01& u)
70 template<class CharT, class Traits>
71 friend std::basic_istream<CharT,Traits>&
72 operator>>(std::basic_istream<CharT,Traits>& is, uniform_01& u)
80 typedef typename base_type::result_type base_result;
85 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
86 // A definition is required even for integral static constants
87 template<class UniformRandomNumberGenerator, class RealType>
88 const bool uniform_01<UniformRandomNumberGenerator, RealType>::has_fixed_range;
93 #endif // BOOST_RANDOM_UNIFORM_01_HPP