Chris@16
|
1 /* boost random/linear_congruential.hpp header file
|
Chris@16
|
2 *
|
Chris@16
|
3 * Copyright Jens Maurer 2000-2001
|
Chris@16
|
4 * Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
5 * accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 *
|
Chris@16
|
8 * See http://www.boost.org for most recent version including documentation.
|
Chris@16
|
9 *
|
Chris@101
|
10 * $Id$
|
Chris@16
|
11 *
|
Chris@16
|
12 * Revision history
|
Chris@16
|
13 * 2001-02-18 moved to individual header files
|
Chris@16
|
14 */
|
Chris@16
|
15
|
Chris@16
|
16 #ifndef BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP
|
Chris@16
|
17 #define BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP
|
Chris@16
|
18
|
Chris@16
|
19 #include <iostream>
|
Chris@16
|
20 #include <stdexcept>
|
Chris@16
|
21 #include <boost/assert.hpp>
|
Chris@16
|
22 #include <boost/config.hpp>
|
Chris@16
|
23 #include <boost/cstdint.hpp>
|
Chris@16
|
24 #include <boost/limits.hpp>
|
Chris@16
|
25 #include <boost/static_assert.hpp>
|
Chris@16
|
26 #include <boost/integer/static_log2.hpp>
|
Chris@16
|
27 #include <boost/mpl/if.hpp>
|
Chris@16
|
28 #include <boost/type_traits/is_arithmetic.hpp>
|
Chris@16
|
29 #include <boost/random/detail/config.hpp>
|
Chris@16
|
30 #include <boost/random/detail/const_mod.hpp>
|
Chris@16
|
31 #include <boost/random/detail/seed.hpp>
|
Chris@16
|
32 #include <boost/random/detail/seed_impl.hpp>
|
Chris@16
|
33 #include <boost/detail/workaround.hpp>
|
Chris@16
|
34
|
Chris@16
|
35 #include <boost/random/detail/disable_warnings.hpp>
|
Chris@16
|
36
|
Chris@16
|
37 namespace boost {
|
Chris@16
|
38 namespace random {
|
Chris@16
|
39
|
Chris@16
|
40 /**
|
Chris@16
|
41 * Instantiations of class template linear_congruential_engine model a
|
Chris@16
|
42 * \pseudo_random_number_generator. Linear congruential pseudo-random
|
Chris@16
|
43 * number generators are described in:
|
Chris@16
|
44 *
|
Chris@16
|
45 * @blockquote
|
Chris@16
|
46 * "Mathematical methods in large-scale computing units", D. H. Lehmer,
|
Chris@16
|
47 * Proc. 2nd Symposium on Large-Scale Digital Calculating Machines,
|
Chris@16
|
48 * Harvard University Press, 1951, pp. 141-146
|
Chris@16
|
49 * @endblockquote
|
Chris@16
|
50 *
|
Chris@16
|
51 * Let x(n) denote the sequence of numbers returned by some pseudo-random
|
Chris@16
|
52 * number generator. Then for the linear congruential generator,
|
Chris@16
|
53 * x(n+1) := (a * x(n) + c) mod m. Parameters for the generator are
|
Chris@16
|
54 * x(0), a, c, m. The template parameter IntType shall denote an integral
|
Chris@16
|
55 * type. It must be large enough to hold values a, c, and m. The template
|
Chris@16
|
56 * parameters a and c must be smaller than m.
|
Chris@16
|
57 *
|
Chris@16
|
58 * Note: The quality of the generator crucially depends on the choice of
|
Chris@16
|
59 * the parameters. User code should use one of the sensibly parameterized
|
Chris@16
|
60 * generators such as minstd_rand instead.
|
Chris@16
|
61 */
|
Chris@16
|
62 template<class IntType, IntType a, IntType c, IntType m>
|
Chris@16
|
63 class linear_congruential_engine
|
Chris@16
|
64 {
|
Chris@16
|
65 public:
|
Chris@16
|
66 typedef IntType result_type;
|
Chris@16
|
67
|
Chris@16
|
68 // Required for old Boost.Random concept
|
Chris@16
|
69 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
|
Chris@16
|
70
|
Chris@16
|
71 BOOST_STATIC_CONSTANT(IntType, multiplier = a);
|
Chris@16
|
72 BOOST_STATIC_CONSTANT(IntType, increment = c);
|
Chris@16
|
73 BOOST_STATIC_CONSTANT(IntType, modulus = m);
|
Chris@16
|
74 BOOST_STATIC_CONSTANT(IntType, default_seed = 1);
|
Chris@16
|
75
|
Chris@16
|
76 BOOST_STATIC_ASSERT(std::numeric_limits<IntType>::is_integer);
|
Chris@16
|
77 BOOST_STATIC_ASSERT(m == 0 || a < m);
|
Chris@16
|
78 BOOST_STATIC_ASSERT(m == 0 || c < m);
|
Chris@16
|
79
|
Chris@16
|
80 /**
|
Chris@16
|
81 * Constructs a @c linear_congruential_engine, using the default seed
|
Chris@16
|
82 */
|
Chris@16
|
83 linear_congruential_engine() { seed(); }
|
Chris@16
|
84
|
Chris@16
|
85 /**
|
Chris@16
|
86 * Constructs a @c linear_congruential_engine, seeding it with @c x0.
|
Chris@16
|
87 */
|
Chris@16
|
88 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(linear_congruential_engine,
|
Chris@16
|
89 IntType, x0)
|
Chris@16
|
90 { seed(x0); }
|
Chris@16
|
91
|
Chris@16
|
92 /**
|
Chris@16
|
93 * Constructs a @c linear_congruential_engine, seeding it with values
|
Chris@16
|
94 * produced by a call to @c seq.generate().
|
Chris@16
|
95 */
|
Chris@16
|
96 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(linear_congruential_engine,
|
Chris@16
|
97 SeedSeq, seq)
|
Chris@16
|
98 { seed(seq); }
|
Chris@16
|
99
|
Chris@16
|
100 /**
|
Chris@16
|
101 * Constructs a @c linear_congruential_engine and seeds it
|
Chris@16
|
102 * with values taken from the itrator range [first, last)
|
Chris@16
|
103 * and adjusts first to point to the element after the last one
|
Chris@16
|
104 * used. If there are not enough elements, throws @c std::invalid_argument.
|
Chris@16
|
105 *
|
Chris@16
|
106 * first and last must be input iterators.
|
Chris@16
|
107 */
|
Chris@16
|
108 template<class It>
|
Chris@16
|
109 linear_congruential_engine(It& first, It last)
|
Chris@16
|
110 {
|
Chris@16
|
111 seed(first, last);
|
Chris@16
|
112 }
|
Chris@16
|
113
|
Chris@16
|
114 // compiler-generated copy constructor and assignment operator are fine
|
Chris@16
|
115
|
Chris@16
|
116 /**
|
Chris@16
|
117 * Calls seed(default_seed)
|
Chris@16
|
118 */
|
Chris@16
|
119 void seed() { seed(default_seed); }
|
Chris@16
|
120
|
Chris@16
|
121 /**
|
Chris@16
|
122 * If c mod m is zero and x0 mod m is zero, changes the current value of
|
Chris@16
|
123 * the generator to 1. Otherwise, changes it to x0 mod m. If c is zero,
|
Chris@16
|
124 * distinct seeds in the range [1,m) will leave the generator in distinct
|
Chris@16
|
125 * states. If c is not zero, the range is [0,m).
|
Chris@16
|
126 */
|
Chris@16
|
127 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(linear_congruential_engine, IntType, x0)
|
Chris@16
|
128 {
|
Chris@16
|
129 // wrap _x if it doesn't fit in the destination
|
Chris@16
|
130 if(modulus == 0) {
|
Chris@16
|
131 _x = x0;
|
Chris@16
|
132 } else {
|
Chris@16
|
133 _x = x0 % modulus;
|
Chris@16
|
134 }
|
Chris@16
|
135 // handle negative seeds
|
Chris@16
|
136 if(_x <= 0 && _x != 0) {
|
Chris@16
|
137 _x += modulus;
|
Chris@16
|
138 }
|
Chris@16
|
139 // adjust to the correct range
|
Chris@16
|
140 if(increment == 0 && _x == 0) {
|
Chris@16
|
141 _x = 1;
|
Chris@16
|
142 }
|
Chris@16
|
143 BOOST_ASSERT(_x >= (min)());
|
Chris@16
|
144 BOOST_ASSERT(_x <= (max)());
|
Chris@16
|
145 }
|
Chris@16
|
146
|
Chris@16
|
147 /**
|
Chris@16
|
148 * Seeds a @c linear_congruential_engine using values from a SeedSeq.
|
Chris@16
|
149 */
|
Chris@16
|
150 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(linear_congruential_engine, SeedSeq, seq)
|
Chris@16
|
151 { seed(detail::seed_one_int<IntType, m>(seq)); }
|
Chris@16
|
152
|
Chris@16
|
153 /**
|
Chris@16
|
154 * seeds a @c linear_congruential_engine with values taken
|
Chris@16
|
155 * from the itrator range [first, last) and adjusts @c first to
|
Chris@16
|
156 * point to the element after the last one used. If there are
|
Chris@16
|
157 * not enough elements, throws @c std::invalid_argument.
|
Chris@16
|
158 *
|
Chris@16
|
159 * @c first and @c last must be input iterators.
|
Chris@16
|
160 */
|
Chris@16
|
161 template<class It>
|
Chris@16
|
162 void seed(It& first, It last)
|
Chris@16
|
163 { seed(detail::get_one_int<IntType, m>(first, last)); }
|
Chris@16
|
164
|
Chris@16
|
165 /**
|
Chris@16
|
166 * Returns the smallest value that the @c linear_congruential_engine
|
Chris@16
|
167 * can produce.
|
Chris@16
|
168 */
|
Chris@16
|
169 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
|
Chris@16
|
170 { return c == 0 ? 1 : 0; }
|
Chris@16
|
171 /**
|
Chris@16
|
172 * Returns the largest value that the @c linear_congruential_engine
|
Chris@16
|
173 * can produce.
|
Chris@16
|
174 */
|
Chris@16
|
175 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
|
Chris@16
|
176 { return modulus-1; }
|
Chris@16
|
177
|
Chris@16
|
178 /** Returns the next value of the @c linear_congruential_engine. */
|
Chris@16
|
179 IntType operator()()
|
Chris@16
|
180 {
|
Chris@16
|
181 _x = const_mod<IntType, m>::mult_add(a, _x, c);
|
Chris@16
|
182 return _x;
|
Chris@16
|
183 }
|
Chris@16
|
184
|
Chris@16
|
185 /** Fills a range with random values */
|
Chris@16
|
186 template<class Iter>
|
Chris@16
|
187 void generate(Iter first, Iter last)
|
Chris@16
|
188 { detail::generate_from_int(*this, first, last); }
|
Chris@16
|
189
|
Chris@16
|
190 /** Advances the state of the generator by @c z. */
|
Chris@16
|
191 void discard(boost::uintmax_t z)
|
Chris@16
|
192 {
|
Chris@16
|
193 typedef const_mod<IntType, m> mod_type;
|
Chris@16
|
194 IntType b_inv = mod_type::invert(a-1);
|
Chris@16
|
195 IntType b_gcd = mod_type::mult(a-1, b_inv);
|
Chris@16
|
196 if(b_gcd == 1) {
|
Chris@16
|
197 IntType a_z = mod_type::pow(a, z);
|
Chris@16
|
198 _x = mod_type::mult_add(a_z, _x,
|
Chris@16
|
199 mod_type::mult(mod_type::mult(c, b_inv), a_z - 1));
|
Chris@16
|
200 } else {
|
Chris@16
|
201 // compute (a^z - 1)*c % (b_gcd * m) / (b / b_gcd) * inv(b / b_gcd)
|
Chris@16
|
202 // we're storing the intermediate result / b_gcd
|
Chris@16
|
203 IntType a_zm1_over_gcd = 0;
|
Chris@16
|
204 IntType a_km1_over_gcd = (a - 1) / b_gcd;
|
Chris@16
|
205 boost::uintmax_t exponent = z;
|
Chris@16
|
206 while(exponent != 0) {
|
Chris@16
|
207 if(exponent % 2 == 1) {
|
Chris@16
|
208 a_zm1_over_gcd =
|
Chris@16
|
209 mod_type::mult_add(
|
Chris@16
|
210 b_gcd,
|
Chris@16
|
211 mod_type::mult(a_zm1_over_gcd, a_km1_over_gcd),
|
Chris@16
|
212 mod_type::add(a_zm1_over_gcd, a_km1_over_gcd));
|
Chris@16
|
213 }
|
Chris@16
|
214 a_km1_over_gcd = mod_type::mult_add(
|
Chris@16
|
215 b_gcd,
|
Chris@16
|
216 mod_type::mult(a_km1_over_gcd, a_km1_over_gcd),
|
Chris@16
|
217 mod_type::add(a_km1_over_gcd, a_km1_over_gcd));
|
Chris@16
|
218 exponent /= 2;
|
Chris@16
|
219 }
|
Chris@16
|
220
|
Chris@16
|
221 IntType a_z = mod_type::mult_add(b_gcd, a_zm1_over_gcd, 1);
|
Chris@16
|
222 IntType num = mod_type::mult(c, a_zm1_over_gcd);
|
Chris@16
|
223 b_inv = mod_type::invert((a-1)/b_gcd);
|
Chris@16
|
224 _x = mod_type::mult_add(a_z, _x, mod_type::mult(b_inv, num));
|
Chris@16
|
225 }
|
Chris@16
|
226 }
|
Chris@16
|
227
|
Chris@16
|
228 friend bool operator==(const linear_congruential_engine& x,
|
Chris@16
|
229 const linear_congruential_engine& y)
|
Chris@16
|
230 { return x._x == y._x; }
|
Chris@16
|
231 friend bool operator!=(const linear_congruential_engine& x,
|
Chris@16
|
232 const linear_congruential_engine& y)
|
Chris@16
|
233 { return !(x == y); }
|
Chris@16
|
234
|
Chris@16
|
235 #if !defined(BOOST_RANDOM_NO_STREAM_OPERATORS)
|
Chris@16
|
236 /** Writes a @c linear_congruential_engine to a @c std::ostream. */
|
Chris@16
|
237 template<class CharT, class Traits>
|
Chris@16
|
238 friend std::basic_ostream<CharT,Traits>&
|
Chris@16
|
239 operator<<(std::basic_ostream<CharT,Traits>& os,
|
Chris@16
|
240 const linear_congruential_engine& lcg)
|
Chris@16
|
241 {
|
Chris@16
|
242 return os << lcg._x;
|
Chris@16
|
243 }
|
Chris@16
|
244
|
Chris@16
|
245 /** Reads a @c linear_congruential_engine from a @c std::istream. */
|
Chris@16
|
246 template<class CharT, class Traits>
|
Chris@16
|
247 friend std::basic_istream<CharT,Traits>&
|
Chris@16
|
248 operator>>(std::basic_istream<CharT,Traits>& is,
|
Chris@16
|
249 linear_congruential_engine& lcg)
|
Chris@16
|
250 {
|
Chris@16
|
251 lcg.read(is);
|
Chris@16
|
252 return is;
|
Chris@16
|
253 }
|
Chris@16
|
254 #endif
|
Chris@16
|
255
|
Chris@16
|
256 private:
|
Chris@16
|
257
|
Chris@16
|
258 /// \cond show_private
|
Chris@16
|
259
|
Chris@16
|
260 template<class CharT, class Traits>
|
Chris@16
|
261 void read(std::basic_istream<CharT, Traits>& is) {
|
Chris@16
|
262 IntType x;
|
Chris@16
|
263 if(is >> x) {
|
Chris@16
|
264 if(x >= (min)() && x <= (max)()) {
|
Chris@16
|
265 _x = x;
|
Chris@16
|
266 } else {
|
Chris@16
|
267 is.setstate(std::ios_base::failbit);
|
Chris@16
|
268 }
|
Chris@16
|
269 }
|
Chris@16
|
270 }
|
Chris@16
|
271
|
Chris@16
|
272 /// \endcond
|
Chris@16
|
273
|
Chris@16
|
274 IntType _x;
|
Chris@16
|
275 };
|
Chris@16
|
276
|
Chris@16
|
277 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
|
Chris@16
|
278 // A definition is required even for integral static constants
|
Chris@16
|
279 template<class IntType, IntType a, IntType c, IntType m>
|
Chris@16
|
280 const bool linear_congruential_engine<IntType, a, c, m>::has_fixed_range;
|
Chris@16
|
281 template<class IntType, IntType a, IntType c, IntType m>
|
Chris@16
|
282 const IntType linear_congruential_engine<IntType,a,c,m>::multiplier;
|
Chris@16
|
283 template<class IntType, IntType a, IntType c, IntType m>
|
Chris@16
|
284 const IntType linear_congruential_engine<IntType,a,c,m>::increment;
|
Chris@16
|
285 template<class IntType, IntType a, IntType c, IntType m>
|
Chris@16
|
286 const IntType linear_congruential_engine<IntType,a,c,m>::modulus;
|
Chris@16
|
287 template<class IntType, IntType a, IntType c, IntType m>
|
Chris@16
|
288 const IntType linear_congruential_engine<IntType,a,c,m>::default_seed;
|
Chris@16
|
289 #endif
|
Chris@16
|
290
|
Chris@16
|
291 /// \cond show_deprecated
|
Chris@16
|
292
|
Chris@16
|
293 // provided for backwards compatibility
|
Chris@16
|
294 template<class IntType, IntType a, IntType c, IntType m, IntType val = 0>
|
Chris@16
|
295 class linear_congruential : public linear_congruential_engine<IntType, a, c, m>
|
Chris@16
|
296 {
|
Chris@16
|
297 typedef linear_congruential_engine<IntType, a, c, m> base_type;
|
Chris@16
|
298 public:
|
Chris@16
|
299 linear_congruential(IntType x0 = 1) : base_type(x0) {}
|
Chris@16
|
300 template<class It>
|
Chris@16
|
301 linear_congruential(It& first, It last) : base_type(first, last) {}
|
Chris@16
|
302 };
|
Chris@16
|
303
|
Chris@16
|
304 /// \endcond
|
Chris@16
|
305
|
Chris@16
|
306 /**
|
Chris@16
|
307 * The specialization \minstd_rand0 was originally suggested in
|
Chris@16
|
308 *
|
Chris@16
|
309 * @blockquote
|
Chris@16
|
310 * A pseudo-random number generator for the System/360, P.A. Lewis,
|
Chris@16
|
311 * A.S. Goodman, J.M. Miller, IBM Systems Journal, Vol. 8, No. 2,
|
Chris@16
|
312 * 1969, pp. 136-146
|
Chris@16
|
313 * @endblockquote
|
Chris@16
|
314 *
|
Chris@16
|
315 * It is examined more closely together with \minstd_rand in
|
Chris@16
|
316 *
|
Chris@16
|
317 * @blockquote
|
Chris@16
|
318 * "Random Number Generators: Good ones are hard to find",
|
Chris@16
|
319 * Stephen K. Park and Keith W. Miller, Communications of
|
Chris@16
|
320 * the ACM, Vol. 31, No. 10, October 1988, pp. 1192-1201
|
Chris@16
|
321 * @endblockquote
|
Chris@16
|
322 */
|
Chris@16
|
323 typedef linear_congruential_engine<uint32_t, 16807, 0, 2147483647> minstd_rand0;
|
Chris@16
|
324
|
Chris@16
|
325 /** The specialization \minstd_rand was suggested in
|
Chris@16
|
326 *
|
Chris@16
|
327 * @blockquote
|
Chris@16
|
328 * "Random Number Generators: Good ones are hard to find",
|
Chris@16
|
329 * Stephen K. Park and Keith W. Miller, Communications of
|
Chris@16
|
330 * the ACM, Vol. 31, No. 10, October 1988, pp. 1192-1201
|
Chris@16
|
331 * @endblockquote
|
Chris@16
|
332 */
|
Chris@16
|
333 typedef linear_congruential_engine<uint32_t, 48271, 0, 2147483647> minstd_rand;
|
Chris@16
|
334
|
Chris@16
|
335
|
Chris@16
|
336 #if !defined(BOOST_NO_INT64_T) && !defined(BOOST_NO_INTEGRAL_INT64_T)
|
Chris@16
|
337 /**
|
Chris@16
|
338 * Class @c rand48 models a \pseudo_random_number_generator. It uses
|
Chris@16
|
339 * the linear congruential algorithm with the parameters a = 0x5DEECE66D,
|
Chris@16
|
340 * c = 0xB, m = 2**48. It delivers identical results to the @c lrand48()
|
Chris@16
|
341 * function available on some systems (assuming lcong48 has not been called).
|
Chris@16
|
342 *
|
Chris@16
|
343 * It is only available on systems where @c uint64_t is provided as an
|
Chris@16
|
344 * integral type, so that for example static in-class constants and/or
|
Chris@16
|
345 * enum definitions with large @c uint64_t numbers work.
|
Chris@16
|
346 */
|
Chris@16
|
347 class rand48
|
Chris@16
|
348 {
|
Chris@16
|
349 public:
|
Chris@16
|
350 typedef boost::uint32_t result_type;
|
Chris@16
|
351
|
Chris@16
|
352 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
|
Chris@16
|
353 /**
|
Chris@16
|
354 * Returns the smallest value that the generator can produce
|
Chris@16
|
355 */
|
Chris@16
|
356 static uint32_t min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }
|
Chris@16
|
357 /**
|
Chris@16
|
358 * Returns the largest value that the generator can produce
|
Chris@16
|
359 */
|
Chris@16
|
360 static uint32_t max BOOST_PREVENT_MACRO_SUBSTITUTION ()
|
Chris@16
|
361 { return 0x7FFFFFFF; }
|
Chris@16
|
362
|
Chris@16
|
363 /** Seeds the generator with the default seed. */
|
Chris@16
|
364 rand48() : lcf(cnv(static_cast<uint32_t>(1))) {}
|
Chris@16
|
365 /**
|
Chris@16
|
366 * Constructs a \rand48 generator with x(0) := (x0 << 16) | 0x330e.
|
Chris@16
|
367 */
|
Chris@16
|
368 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(rand48, result_type, x0)
|
Chris@16
|
369 { seed(x0); }
|
Chris@16
|
370 /**
|
Chris@16
|
371 * Seeds the generator with values produced by @c seq.generate().
|
Chris@16
|
372 */
|
Chris@16
|
373 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(rand48, SeedSeq, seq)
|
Chris@16
|
374 { seed(seq); }
|
Chris@16
|
375 /**
|
Chris@16
|
376 * Seeds the generator using values from an iterator range,
|
Chris@16
|
377 * and updates first to point one past the last value consumed.
|
Chris@16
|
378 */
|
Chris@16
|
379 template<class It> rand48(It& first, It last) : lcf(first, last) { }
|
Chris@16
|
380
|
Chris@16
|
381 // compiler-generated copy ctor and assignment operator are fine
|
Chris@16
|
382
|
Chris@16
|
383 /** Seeds the generator with the default seed. */
|
Chris@16
|
384 void seed() { seed(static_cast<uint32_t>(1)); }
|
Chris@16
|
385 /**
|
Chris@16
|
386 * Changes the current value x(n) of the generator to (x0 << 16) | 0x330e.
|
Chris@16
|
387 */
|
Chris@16
|
388 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(rand48, result_type, x0)
|
Chris@16
|
389 { lcf.seed(cnv(x0)); }
|
Chris@16
|
390 /**
|
Chris@16
|
391 * Seeds the generator using values from an iterator range,
|
Chris@16
|
392 * and updates first to point one past the last value consumed.
|
Chris@16
|
393 */
|
Chris@16
|
394 template<class It> void seed(It& first, It last) { lcf.seed(first,last); }
|
Chris@16
|
395 /**
|
Chris@16
|
396 * Seeds the generator with values produced by @c seq.generate().
|
Chris@16
|
397 */
|
Chris@16
|
398 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(rand48, SeedSeq, seq)
|
Chris@16
|
399 { lcf.seed(seq); }
|
Chris@16
|
400
|
Chris@16
|
401 /** Returns the next value of the generator. */
|
Chris@16
|
402 uint32_t operator()() { return static_cast<uint32_t>(lcf() >> 17); }
|
Chris@16
|
403
|
Chris@16
|
404 /** Advances the state of the generator by @c z. */
|
Chris@16
|
405 void discard(boost::uintmax_t z) { lcf.discard(z); }
|
Chris@16
|
406
|
Chris@16
|
407 /** Fills a range with random values */
|
Chris@16
|
408 template<class Iter>
|
Chris@16
|
409 void generate(Iter first, Iter last)
|
Chris@16
|
410 {
|
Chris@16
|
411 for(; first != last; ++first) {
|
Chris@16
|
412 *first = (*this)();
|
Chris@16
|
413 }
|
Chris@16
|
414 }
|
Chris@16
|
415
|
Chris@16
|
416 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
|
Chris@16
|
417 /** Writes a @c rand48 to a @c std::ostream. */
|
Chris@16
|
418 template<class CharT,class Traits>
|
Chris@16
|
419 friend std::basic_ostream<CharT,Traits>&
|
Chris@16
|
420 operator<<(std::basic_ostream<CharT,Traits>& os, const rand48& r)
|
Chris@16
|
421 { os << r.lcf; return os; }
|
Chris@16
|
422
|
Chris@16
|
423 /** Reads a @c rand48 from a @c std::istream. */
|
Chris@16
|
424 template<class CharT,class Traits>
|
Chris@16
|
425 friend std::basic_istream<CharT,Traits>&
|
Chris@16
|
426 operator>>(std::basic_istream<CharT,Traits>& is, rand48& r)
|
Chris@16
|
427 { is >> r.lcf; return is; }
|
Chris@16
|
428 #endif
|
Chris@16
|
429
|
Chris@16
|
430 /**
|
Chris@16
|
431 * Returns true if the two generators will produce identical
|
Chris@16
|
432 * sequences of values.
|
Chris@16
|
433 */
|
Chris@16
|
434 friend bool operator==(const rand48& x, const rand48& y)
|
Chris@16
|
435 { return x.lcf == y.lcf; }
|
Chris@16
|
436 /**
|
Chris@16
|
437 * Returns true if the two generators will produce different
|
Chris@16
|
438 * sequences of values.
|
Chris@16
|
439 */
|
Chris@16
|
440 friend bool operator!=(const rand48& x, const rand48& y)
|
Chris@16
|
441 { return !(x == y); }
|
Chris@16
|
442 private:
|
Chris@16
|
443 /// \cond show_private
|
Chris@16
|
444 typedef random::linear_congruential_engine<uint64_t,
|
Chris@16
|
445 // xxxxULL is not portable
|
Chris@16
|
446 uint64_t(0xDEECE66DUL) | (uint64_t(0x5) << 32),
|
Chris@16
|
447 0xB, uint64_t(1)<<48> lcf_t;
|
Chris@16
|
448 lcf_t lcf;
|
Chris@16
|
449
|
Chris@16
|
450 static boost::uint64_t cnv(boost::uint32_t x)
|
Chris@16
|
451 { return (static_cast<uint64_t>(x) << 16) | 0x330e; }
|
Chris@16
|
452 /// \endcond
|
Chris@16
|
453 };
|
Chris@16
|
454 #endif /* !BOOST_NO_INT64_T && !BOOST_NO_INTEGRAL_INT64_T */
|
Chris@16
|
455
|
Chris@16
|
456 } // namespace random
|
Chris@16
|
457
|
Chris@16
|
458 using random::minstd_rand0;
|
Chris@16
|
459 using random::minstd_rand;
|
Chris@16
|
460 using random::rand48;
|
Chris@16
|
461
|
Chris@16
|
462 } // namespace boost
|
Chris@16
|
463
|
Chris@16
|
464 #include <boost/random/detail/enable_warnings.hpp>
|
Chris@16
|
465
|
Chris@16
|
466 #endif // BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP
|