1 /* boost random/xor_combine.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: xor_combine.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
14 #ifndef BOOST_RANDOM_XOR_COMBINE_HPP
15 #define BOOST_RANDOM_XOR_COMBINE_HPP
20 #include <algorithm> // for std::min and std::max
21 #include <boost/config.hpp>
22 #include <boost/limits.hpp>
23 #include <boost/cstdint.hpp> // uint32_t
24 #include <boost/random/detail/config.hpp>
25 #include <boost/random/detail/seed.hpp>
26 #include <boost/random/detail/seed_impl.hpp>
32 * Instantiations of @c xor_combine_engine model a
33 * \pseudo_random_number_generator. To produce its output it
34 * invokes each of the base generators, shifts their results
35 * and xors them together.
37 template<class URNG1, int s1, class URNG2, int s2>
38 class xor_combine_engine
41 typedef URNG1 base1_type;
42 typedef URNG2 base2_type;
43 typedef typename base1_type::result_type result_type;
45 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
46 BOOST_STATIC_CONSTANT(int, shift1 = s1);
47 BOOST_STATIC_CONSTANT(int, shift2 = s2);
50 * Constructors a @c xor_combine_engine by default constructing
51 * both base generators.
53 xor_combine_engine() : _rng1(), _rng2() { }
55 /** Constructs a @c xor_combine by copying two base generators. */
56 xor_combine_engine(const base1_type & rng1, const base2_type & rng2)
57 : _rng1(rng1), _rng2(rng2) { }
60 * Constructs a @c xor_combine_engine, seeding both base generators
64 * The exact algorithm used by this function may change in the future.
67 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(xor_combine_engine,
72 * Constructs a @c xor_combine_engine, seeding both base generators
73 * with values produced by @c seq.
75 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(xor_combine_engine,
80 * Constructs a @c xor_combine_engine, seeding both base generators
81 * with values from the iterator range [first, last) and changes
82 * first to point to the element after the last one used. If there
83 * are not enough elements in the range to seed both generators,
84 * throws @c std::invalid_argument.
86 template<class It> xor_combine_engine(It& first, It last)
87 : _rng1(first, last), _rng2( /* advanced by other call */ first, last) { }
89 /** Calls @c seed() for both base generators. */
90 void seed() { _rng1.seed(); _rng2.seed(); }
92 /** @c seeds both base generators with @c v. */
93 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(xor_combine_engine, result_type, v)
94 { _rng1.seed(v); _rng2.seed(v); }
96 /** @c seeds both base generators with values produced by @c seq. */
97 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(xor_combine_engine, SeedSeq, seq)
98 { _rng1.seed(seq); _rng2.seed(seq); }
101 * seeds both base generators with values from the iterator
102 * range [first, last) and changes first to point to the element
103 * after the last one used. If there are not enough elements in
104 * the range to seed both generators, throws @c std::invalid_argument.
106 template<class It> void seed(It& first, It last)
108 _rng1.seed(first, last);
109 _rng2.seed(first, last);
112 /** Returns the first base generator. */
113 const base1_type& base1() const { return _rng1; }
115 /** Returns the second base generator. */
116 const base2_type& base2() const { return _rng2; }
118 /** Returns the next value of the generator. */
119 result_type operator()()
121 return (_rng1() << s1) ^ (_rng2() << s2);
124 /** Fills a range with random values */
126 void generate(Iter first, Iter last)
127 { detail::generate_from_int(*this, first, last); }
129 /** Advances the state of the generator by @c z. */
130 void discard(boost::uintmax_t z)
136 /** Returns the smallest value that the generator can produce. */
137 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return (std::min)((URNG1::min)(), (URNG2::min)()); }
138 /** Returns the largest value that the generator can produce. */
139 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () { return (std::max)((URNG1::min)(), (URNG2::max)()); }
142 * Writes the textual representation of the generator to a @c std::ostream.
144 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, xor_combine_engine, s)
146 os << s._rng1 << ' ' << s._rng2;
151 * Reads the textual representation of the generator from a @c std::istream.
153 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, xor_combine_engine, s)
155 is >> s._rng1 >> std::ws >> s._rng2;
159 /** Returns true if the two generators will produce identical sequences. */
160 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(xor_combine_engine, x, y)
161 { return x._rng1 == y._rng1 && x._rng2 == y._rng2; }
163 /** Returns true if the two generators will produce different sequences. */
164 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(xor_combine_engine)
171 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
172 // A definition is required even for integral static constants
173 template<class URNG1, int s1, class URNG2, int s2>
174 const bool xor_combine_engine<URNG1, s1, URNG2, s2>::has_fixed_range;
175 template<class URNG1, int s1, class URNG2, int s2>
176 const int xor_combine_engine<URNG1, s1, URNG2, s2>::shift1;
177 template<class URNG1, int s1, class URNG2, int s2>
178 const int xor_combine_engine<URNG1, s1, URNG2, s2>::shift2;
181 /// \cond show_private
183 /** Provided for backwards compatibility. */
184 template<class URNG1, int s1, class URNG2, int s2,
185 typename URNG1::result_type v = 0>
186 class xor_combine : public xor_combine_engine<URNG1, s1, URNG2, s2>
188 typedef xor_combine_engine<URNG1, s1, URNG2, s2> base_type;
190 typedef typename base_type::result_type result_type;
192 xor_combine(result_type val) : base_type(val) {}
194 xor_combine(It& first, It last) : base_type(first, last) {}
195 xor_combine(const URNG1 & rng1, const URNG2 & rng2)
196 : base_type(rng1, rng2) { }
198 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (std::min)((this->base1().min)(), (this->base2().min)()); }
199 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (std::max)((this->base1().min)(), (this->base2().max)()); }
204 } // namespace random
207 #endif // BOOST_RANDOM_XOR_COMBINE_HPP