1 /* boost random/additive_combine.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: additive_combine.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_ADDITIVE_COMBINE_HPP
17 #define BOOST_RANDOM_ADDITIVE_COMBINE_HPP
21 #include <algorithm> // for std::min and std::max
22 #include <boost/config.hpp>
23 #include <boost/cstdint.hpp>
24 #include <boost/random/detail/config.hpp>
25 #include <boost/random/detail/operators.hpp>
26 #include <boost/random/detail/seed.hpp>
27 #include <boost/random/linear_congruential.hpp>
33 * An instantiation of class template @c additive_combine_engine models a
34 * \pseudo_random_number_generator. It combines two multiplicative
35 * \linear_congruential_engine number generators, i.e. those with @c c = 0.
39 * "Efficient and Portable Combined Random Number Generators", Pierre L'Ecuyer,
40 * Communications of the ACM, Vol. 31, No. 6, June 1988, pp. 742-749, 774
43 * The template parameters MLCG1 and MLCG2 shall denote two different
44 * \linear_congruential_engine number generators, each with c = 0. Each
45 * invocation returns a random number
46 * X(n) := (MLCG1(n) - MLCG2(n)) mod (m1 - 1),
47 * where m1 denotes the modulus of MLCG1.
49 template<class MLCG1, class MLCG2>
50 class additive_combine_engine
53 typedef MLCG1 first_base;
54 typedef MLCG2 second_base;
55 typedef typename MLCG1::result_type result_type;
57 // Required by old Boost.Random concept
58 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
60 * Returns the smallest value that the generator can produce
62 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
65 * Returns the largest value that the generator can produce
67 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
68 { return MLCG1::modulus-1; }
71 * Constructs an @c additive_combine_engine using the
72 * default constructors of the two base generators.
74 additive_combine_engine() : _mlcg1(), _mlcg2() { }
76 * Constructs an @c additive_combine_engine, using seed as
77 * the constructor argument for both base generators.
79 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(additive_combine_engine,
80 result_type, seed_arg)
82 _mlcg1.seed(seed_arg);
83 _mlcg2.seed(seed_arg);
86 * Constructs an @c additive_combine_engine, using seq as
87 * the constructor argument for both base generators.
90 * The semantics of this function are liable to change.
91 * A @c seed_seq is designed to generate all the seeds
92 * in one shot, but this seeds the two base engines
93 * independantly and probably ends up giving the same
97 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(additive_combine_engine,
104 * Constructs an @c additive_combine_engine, using
105 * @c seed1 and @c seed2 as the constructor argument to
106 * the first and second base generators, respectively.
108 additive_combine_engine(typename MLCG1::result_type seed1,
109 typename MLCG2::result_type seed2)
110 : _mlcg1(seed1), _mlcg2(seed2) { }
112 * Contructs an @c additive_combine_engine with
113 * values from the range defined by the input iterators first
114 * and last. first will be modified to point to the element
115 * after the last one used.
117 * Throws: @c std::invalid_argument if the input range is too small.
119 * Exception Safety: Basic
121 template<class It> additive_combine_engine(It& first, It last)
122 : _mlcg1(first, last), _mlcg2(first, last) { }
125 * Seeds an @c additive_combine_engine using the default
126 * seeds of the two base generators.
135 * Seeds an @c additive_combine_engine, using @c seed as the
136 * seed for both base generators.
138 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(additive_combine_engine,
139 result_type, seed_arg)
141 _mlcg1.seed(seed_arg);
142 _mlcg2.seed(seed_arg);
146 * Seeds an @c additive_combine_engine, using @c seq to
147 * seed both base generators.
149 * See the warning on the corresponding constructor.
151 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(additive_combine_engine,
159 * Seeds an @c additive_combine generator, using @c seed1 and @c seed2 as
160 * the seeds to the first and second base generators, respectively.
162 void seed(typename MLCG1::result_type seed1,
163 typename MLCG2::result_type seed2)
170 * Seeds an @c additive_combine_engine with
171 * values from the range defined by the input iterators first
172 * and last. first will be modified to point to the element
173 * after the last one used.
175 * Throws: @c std::invalid_argument if the input range is too small.
177 * Exception Safety: Basic
179 template<class It> void seed(It& first, It last)
181 _mlcg1.seed(first, last);
182 _mlcg2.seed(first, last);
185 /** Returns the next value of the generator. */
186 result_type operator()() {
187 result_type val1 = _mlcg1();
188 result_type val2 = _mlcg2();
189 if(val2 < val1) return val1 - val2;
190 else return val1 - val2 + MLCG1::modulus - 1;
193 /** Fills a range with random values */
195 void generate(Iter first, Iter last)
196 { detail::generate_from_int(*this, first, last); }
198 /** Advances the state of the generator by @c z. */
199 void discard(boost::uintmax_t z)
206 * Writes the state of an @c additive_combine_engine to a @c
207 * std::ostream. The textual representation of an @c
208 * additive_combine_engine is the textual representation of
209 * the first base generator followed by the textual representation
210 * of the second base generator.
212 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, additive_combine_engine, r)
213 { os << r._mlcg1 << ' ' << r._mlcg2; return os; }
216 * Reads the state of an @c additive_combine_engine from a
219 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, additive_combine_engine, r)
220 { is >> r._mlcg1 >> std::ws >> r._mlcg2; return is; }
223 * Returns: true iff the two @c additive_combine_engines will
224 * produce the same sequence of values.
226 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(additive_combine_engine, x, y)
227 { return x._mlcg1 == y._mlcg1 && x._mlcg2 == y._mlcg2; }
229 * Returns: true iff the two @c additive_combine_engines will
230 * produce different sequences of values.
232 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(additive_combine_engine)
239 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
240 template<class MLCG1, class MLCG2>
241 const bool additive_combine_engine<MLCG1, MLCG2>::has_fixed_range;
244 /// \cond show_deprecated
246 /** Provided for backwards compatibility. */
247 template<class MLCG1, class MLCG2, typename MLCG1::result_type val = 0>
248 class additive_combine : public additive_combine_engine<MLCG1, MLCG2>
250 typedef additive_combine_engine<MLCG1, MLCG2> base_t;
252 typedef typename base_t::result_type result_type;
253 additive_combine() {}
255 additive_combine(T& arg) : base_t(arg) {}
257 additive_combine(const T& arg) : base_t(arg) {}
259 additive_combine(It& first, It last) : base_t(first, last) {}
265 * The specialization \ecuyer1988 was suggested in
268 * "Efficient and Portable Combined Random Number Generators", Pierre L'Ecuyer,
269 * Communications of the ACM, Vol. 31, No. 6, June 1988, pp. 742-749, 774
272 typedef additive_combine_engine<
273 linear_congruential_engine<uint32_t, 40014, 0, 2147483563>,
274 linear_congruential_engine<uint32_t, 40692, 0, 2147483399>
277 } // namespace random
279 using random::ecuyer1988;
283 #endif // BOOST_RANDOM_ADDITIVE_COMBINE_HPP