1 /* boost random/shuffle_output.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: shuffle_output.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_SHUFFLE_OUTPUT_HPP
17 #define BOOST_RANDOM_SHUFFLE_OUTPUT_HPP
20 #include <algorithm> // std::copy
22 #include <boost/config.hpp>
23 #include <boost/limits.hpp>
24 #include <boost/static_assert.hpp>
25 #include <boost/cstdint.hpp>
26 #include <boost/random/linear_congruential.hpp>
32 * Instatiations of class template shuffle_output model a
33 * \pseudo_random_number_generator. It mixes the output
34 * of some (usually \linear_congruential) \uniform_random_number_generator
35 * to get better statistical properties.
36 * The algorithm is described in
39 * "Improving a poor random number generator", Carter Bays
40 * and S.D. Durham, ACM Transactions on Mathematical Software,
41 * Vol 2, No. 1, March 1976, pp. 59-64.
42 * http://doi.acm.org/10.1145/355666.355670
45 * The output of the base generator is buffered in an array of
46 * length k. Every output X(n) has a second role: It gives an
47 * index into the array where X(n+1) will be retrieved. Used
48 * array elements are replaced with fresh output from the base
51 * Template parameters are the base generator and the array
52 * length k, which should be around 100. The template parameter
53 * val is the validation value checked by validation.
55 template<class UniformRandomNumberGenerator, int k,
56 #ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
57 typename UniformRandomNumberGenerator::result_type
65 typedef UniformRandomNumberGenerator base_type;
66 typedef typename base_type::result_type result_type;
68 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
69 BOOST_STATIC_CONSTANT(int, buffer_size = k);
72 * Constructs a @c shuffle_output generator by invoking the
73 * default constructor of the base generator.
75 * Complexity: Exactly k+1 invocations of the base generator.
77 shuffle_output() : _rng() { init(); }
78 #if defined(BOOST_MSVC) && _MSC_VER < 1300
79 // MSVC does not implicitly generate the copy constructor here
80 shuffle_output(const shuffle_output & x)
81 : _rng(x._rng), y(x.y) { std::copy(x.v, x.v+k, v); }
84 * Constructs a shuffle_output generator by invoking the one-argument
85 * constructor of the base generator with the parameter seed.
87 * Complexity: Exactly k+1 invocations of the base generator.
90 explicit shuffle_output(T s) : _rng(s) { init(); }
92 * Constructs a shuffle_output generator by using a copy
93 * of the provided generator.
95 * Precondition: The template argument UniformRandomNumberGenerator
96 * shall denote a CopyConstructible type.
98 * Complexity: Exactly k+1 invocations of the base generator.
100 explicit shuffle_output(const base_type & rng) : _rng(rng) { init(); }
101 template<class It> shuffle_output(It& first, It last)
102 : _rng(first, last) { init(); }
103 void seed() { _rng.seed(); init(); }
105 * Invokes the one-argument seed method of the base generator
106 * with the parameter seed and re-initializes the internal buffer array.
108 * Complexity: Exactly k+1 invocations of the base generator.
111 void seed(T s) { _rng.seed(s); init(); }
112 template<class It> void seed(It& first, It last)
114 _rng.seed(first, last);
118 const base_type& base() const { return _rng; }
120 result_type operator()() {
121 // calculating the range every time may seem wasteful. However, this
122 // makes the information locally available for the optimizer.
123 result_type range = (max)()-(min)()+1;
124 int j = k*(y-(min)())/range;
125 // assert(0 <= j && j < k);
131 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.min)(); }
132 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.max)(); }
133 static bool validation(result_type x) { return val == x; }
135 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
137 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
138 template<class CharT, class Traits>
139 friend std::basic_ostream<CharT,Traits>&
140 operator<<(std::basic_ostream<CharT,Traits>& os, const shuffle_output& s)
142 os << s._rng << " " << s.y << " ";
143 for(int i = 0; i < s.buffer_size; ++i)
148 template<class CharT, class Traits>
149 friend std::basic_istream<CharT,Traits>&
150 operator>>(std::basic_istream<CharT,Traits>& is, shuffle_output& s)
152 is >> s._rng >> std::ws >> s.y >> std::ws;
153 for(int i = 0; i < s.buffer_size; ++i)
154 is >> s.v[i] >> std::ws;
159 friend bool operator==(const shuffle_output& x, const shuffle_output& y)
160 { return x._rng == y._rng && x.y == y.y && std::equal(x.v, x.v+k, y.v); }
161 friend bool operator!=(const shuffle_output& x, const shuffle_output& y)
162 { return !(x == y); }
164 // Use a member function; Streamable concept not supported.
165 bool operator==(const shuffle_output& rhs) const
166 { return _rng == rhs._rng && y == rhs.y && std::equal(v, v+k, rhs.v); }
167 bool operator!=(const shuffle_output& rhs) const
168 { return !(*this == rhs); }
173 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
174 BOOST_STATIC_ASSERT(std::numeric_limits<result_type>::is_integer);
176 result_type range = (max)()-(min)();
177 assert(range > 0); // otherwise there would be little choice
178 if(static_cast<unsigned long>(k * range) <
179 static_cast<unsigned long>(range)) // not a sufficient condition
180 // likely overflow with bucket number computation
181 assert(!"overflow will occur");
183 // we cannot use std::generate, because it uses pass-by-value for _rng
184 for(result_type * p = v; p != v+k; ++p)
194 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
195 // A definition is required even for integral static constants
196 template<class UniformRandomNumberGenerator, int k,
197 #ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
198 typename UniformRandomNumberGenerator::result_type
203 const bool shuffle_output<UniformRandomNumberGenerator, k, val>::has_fixed_range;
205 template<class UniformRandomNumberGenerator, int k,
206 #ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
207 typename UniformRandomNumberGenerator::result_type
212 const int shuffle_output<UniformRandomNumberGenerator, k, val>::buffer_size;
215 } // namespace random
217 // validation by experiment from Harry Erwin's generator.h (private e-mail)
219 * According to Harry Erwin (private e-mail), the specialization
220 * @c kreutzer1986 was suggested in:
223 * "System Simulation: Programming Styles and Languages (International
224 * Computer Science Series)", Wolfgang Kreutzer, Addison-Wesley, December 1986.
227 typedef random::shuffle_output<
228 random::linear_congruential<uint32_t, 1366, 150889, 714025, 0>,
229 97, 139726> kreutzer1986;
234 #endif // BOOST_RANDOM_SHUFFLE_OUTPUT_HPP