1 /* boost random/variate_generator.hpp header file
3 * Copyright Jens Maurer 2002
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: variate_generator.hpp,v 1.8 2005/02/14 11:53:50 johnmaddock Exp $
14 #ifndef BOOST_RANDOM_RANDOM_GENERATOR_HPP
15 #define BOOST_RANDOM_RANDOM_GENERATOR_HPP
17 #include <boost/config.hpp>
19 // implementation details
20 #include <boost/detail/workaround.hpp>
21 #include <boost/random/uniform_01.hpp>
22 #include <boost/random/detail/pass_through_engine.hpp>
23 #include <boost/random/detail/uniform_int_float.hpp>
24 #include <boost/random/detail/ptr_helper.hpp>
26 // Borland C++ 5.6.0 has problems using its numeric_limits traits as
27 // template parameters
28 #if BOOST_WORKAROUND(__BORLANDC__, <= 0x564)
29 #include <boost/type_traits/is_integral.hpp>
37 template<bool have_int, bool want_int>
40 // for consistency, always have two levels of decorations
42 struct engine_helper<true, true>
44 template<class Engine, class DistInputType>
47 typedef pass_through_engine<Engine> type;
52 struct engine_helper<false, false>
54 template<class Engine, class DistInputType>
57 typedef uniform_01<Engine, DistInputType> type;
62 struct engine_helper<true, false>
64 template<class Engine, class DistInputType>
67 typedef uniform_01<Engine, DistInputType> type;
72 struct engine_helper<false, true>
74 template<class Engine, class DistInputType>
77 typedef uniform_int_float<Engine, unsigned long> type;
85 template<class Engine, class Distribution>
86 class variate_generator
89 typedef random::detail::pass_through_engine<Engine> decorated_engine;
92 typedef typename decorated_engine::base_type engine_value_type;
93 typedef Engine engine_type;
94 typedef Distribution distribution_type;
95 typedef typename Distribution::result_type result_type;
97 variate_generator(Engine e, Distribution d)
98 : _eng(decorated_engine(e)), _dist(d) { }
100 result_type operator()() { return _dist(_eng); }
102 result_type operator()(T value) { return _dist(_eng, value); }
104 engine_value_type& engine() { return _eng.base().base(); }
105 const engine_value_type& engine() const { return _eng.base().base(); }
107 distribution_type& distribution() { return _dist; }
108 const distribution_type& distribution() const { return _dist; }
110 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (distribution().min)(); }
111 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (distribution().max)(); }
114 #if BOOST_WORKAROUND(__BORLANDC__, <= 0x564)
115 typedef typename random::detail::engine_helper<
116 boost::is_integral<typename decorated_engine::result_type>::value,
117 boost::is_integral<typename Distribution::input_type>::value
118 >::BOOST_NESTED_TEMPLATE impl<decorated_engine, typename Distribution::input_type>::type internal_engine_type;
121 have_int = std::numeric_limits<typename decorated_engine::result_type>::is_integer,
122 want_int = std::numeric_limits<typename Distribution::input_type>::is_integer
124 typedef typename random::detail::engine_helper<have_int, want_int>::BOOST_NESTED_TEMPLATE impl<decorated_engine, typename Distribution::input_type>::type internal_engine_type;
127 internal_engine_type _eng;
128 distribution_type _dist;
133 #endif // BOOST_RANDOM_RANDOM_GENERATOR_HPP