1 /* boost random/lagged_fibonacci.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: lagged_fibonacci.hpp 72951 2011-07-07 04:57:37Z steven_watanabe $
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_LAGGED_FIBONACCI_HPP
17 #define BOOST_RANDOM_LAGGED_FIBONACCI_HPP
21 #include <algorithm> // std::max
23 #include <boost/config/no_tr1/cmath.hpp> // std::pow
24 #include <boost/config.hpp>
25 #include <boost/limits.hpp>
26 #include <boost/cstdint.hpp>
27 #include <boost/integer/integer_mask.hpp>
28 #include <boost/random/linear_congruential.hpp>
29 #include <boost/random/uniform_01.hpp>
30 #include <boost/random/detail/config.hpp>
31 #include <boost/random/detail/seed.hpp>
32 #include <boost/random/detail/operators.hpp>
33 #include <boost/random/detail/generator_seed_seq.hpp>
39 * Instantiations of class template \lagged_fibonacci_engine model a
40 * \pseudo_random_number_generator. It uses a lagged Fibonacci
41 * algorithm with two lags @c p and @c q:
42 * x(i) = x(i-p) + x(i-q) (mod 2<sup>w</sup>) with p > q.
44 template<class UIntType, int w, unsigned int p, unsigned int q>
45 class lagged_fibonacci_engine
48 typedef UIntType result_type;
49 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
50 BOOST_STATIC_CONSTANT(int, word_size = w);
51 BOOST_STATIC_CONSTANT(unsigned int, long_lag = p);
52 BOOST_STATIC_CONSTANT(unsigned int, short_lag = q);
54 BOOST_STATIC_CONSTANT(UIntType, default_seed = 331u);
56 /** Returns the smallest value that the generator can produce. */
57 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }
58 /** Returns the largest value that the generator can produce. */
59 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
60 { return low_bits_mask_t<w>::sig_bits; }
62 /** Creates a new @c lagged_fibonacci_engine and calls @c seed(). */
63 lagged_fibonacci_engine() { seed(); }
65 /** Creates a new @c lagged_fibonacci_engine and calls @c seed(value). */
66 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(lagged_fibonacci_engine,
70 /** Creates a new @c lagged_fibonacci_engine and calls @c seed(seq). */
71 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(lagged_fibonacci_engine,
76 * Creates a new @c lagged_fibonacci_engine and calls @c seed(first, last).
78 template<class It> lagged_fibonacci_engine(It& first, It last)
79 { seed(first, last); }
81 // compiler-generated copy ctor and assignment operator are fine
83 /** Calls @c seed(default_seed). */
84 void seed() { seed(default_seed); }
87 * Sets the state of the generator to values produced by
88 * a \minstd_rand0 generator.
90 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(lagged_fibonacci_engine,
93 minstd_rand0 intgen(static_cast<boost::uint32_t>(value));
94 detail::generator_seed_seq<minstd_rand0> gen(intgen);
99 * Sets the state of the generator using values produced by seq.
101 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(lagged_fibonacci_engine, SeedSeq, seq)
103 detail::seed_array_int<w>(seq, x);
108 * Sets the state of the generator to values from the iterator
109 * range [first, last). If there are not enough elements in the
110 * range [first, last) throws @c std::invalid_argument.
113 void seed(It& first, It last)
115 detail::fill_array_int<w>(first, last, x);
119 /** Returns the next value of the generator. */
120 result_type operator()()
127 /** Fills a range with random values */
129 void generate(Iter first, Iter last)
130 { detail::generate_from_int(*this, first, last); }
132 /** Advances the state of the generator by @c z. */
133 void discard(boost::uintmax_t z)
135 for(boost::uintmax_t j = 0; j < z; ++j) {
141 * Writes the textual representation of the generator to a @c std::ostream.
143 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, lagged_fibonacci_engine, f)
146 for(unsigned int i = 0; i < f.long_lag; ++i)
152 * Reads the textual representation of the generator from a @c std::istream.
154 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, lagged_fibonacci_engine, f)
156 is >> f.i >> std::ws;
157 for(unsigned int i = 0; i < f.long_lag; ++i)
158 is >> f.x[i] >> std::ws;
163 * Returns true if the two generators will produce identical
164 * sequences of outputs.
166 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(lagged_fibonacci_engine, x, y)
167 { return x.i == y.i && std::equal(x.x, x.x+long_lag, y.x); }
170 * Returns true if the two generators will produce different
171 * sequences of outputs.
173 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(lagged_fibonacci_engine)
176 /// \cond show_private
181 UIntType x[long_lag];
184 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
185 // A definition is required even for integral static constants
186 template<class UIntType, int w, unsigned int p, unsigned int q>
187 const bool lagged_fibonacci_engine<UIntType, w, p, q>::has_fixed_range;
188 template<class UIntType, int w, unsigned int p, unsigned int q>
189 const unsigned int lagged_fibonacci_engine<UIntType, w, p, q>::long_lag;
190 template<class UIntType, int w, unsigned int p, unsigned int q>
191 const unsigned int lagged_fibonacci_engine<UIntType, w, p, q>::short_lag;
192 template<class UIntType, int w, unsigned int p, unsigned int q>
193 const UIntType lagged_fibonacci_engine<UIntType, w, p, q>::default_seed;
196 /// \cond show_private
198 template<class UIntType, int w, unsigned int p, unsigned int q>
199 void lagged_fibonacci_engine<UIntType, w, p, q>::fill()
201 // two loops to avoid costly modulo operations
202 { // extra scope for MSVC brokenness w.r.t. for scope
203 for(unsigned int j = 0; j < short_lag; ++j)
204 x[j] = (x[j] + x[j+(long_lag-short_lag)]) & low_bits_mask_t<w>::sig_bits;
206 for(unsigned int j = short_lag; j < long_lag; ++j)
207 x[j] = (x[j] + x[j-short_lag]) & low_bits_mask_t<w>::sig_bits;
213 /// \cond show_deprecated
215 // provided for backwards compatibility
216 template<class UIntType, int w, unsigned int p, unsigned int q, UIntType v = 0>
217 class lagged_fibonacci : public lagged_fibonacci_engine<UIntType, w, p, q>
219 typedef lagged_fibonacci_engine<UIntType, w, p, q> base_type;
221 lagged_fibonacci() {}
222 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(lagged_fibonacci, UIntType, val)
224 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(lagged_fibonacci, SeedSeq, seq)
227 lagged_fibonacci(It& first, It last) : base_type(first, last) {}
232 // lagged Fibonacci generator for the range [0..1)
233 // contributed by Matthias Troyer
234 // for p=55, q=24 originally by G. J. Mitchell and D. P. Moore 1958
237 * Instantiations of class template @c lagged_fibonacci_01 model a
238 * \pseudo_random_number_generator. It uses a lagged Fibonacci
239 * algorithm with two lags @c p and @c q, evaluated in floating-point
240 * arithmetic: x(i) = x(i-p) + x(i-q) (mod 1) with p > q. See
243 * "Uniform random number generators for supercomputers", Richard Brent,
244 * Proc. of Fifth Australian Supercomputer Conference, Melbourne,
245 * Dec. 1992, pp. 704-706.
249 * The quality of the generator crucially depends on the choice
250 * of the parameters. User code should employ one of the sensibly
251 * parameterized generators such as \lagged_fibonacci607 instead.
254 * The generator requires considerable amounts of memory for the storage
255 * of its state array. For example, \lagged_fibonacci607 requires about
256 * 4856 bytes and \lagged_fibonacci44497 requires about 350 KBytes.
258 template<class RealType, int w, unsigned int p, unsigned int q>
259 class lagged_fibonacci_01_engine
262 typedef RealType result_type;
263 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
264 BOOST_STATIC_CONSTANT(int, word_size = w);
265 BOOST_STATIC_CONSTANT(unsigned int, long_lag = p);
266 BOOST_STATIC_CONSTANT(unsigned int, short_lag = q);
268 BOOST_STATIC_CONSTANT(boost::uint32_t, default_seed = 331u);
270 /** Constructs a @c lagged_fibonacci_01 generator and calls @c seed(). */
271 lagged_fibonacci_01_engine() { seed(); }
272 /** Constructs a @c lagged_fibonacci_01 generator and calls @c seed(value). */
273 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(lagged_fibonacci_01_engine, uint32_t, value)
275 /** Constructs a @c lagged_fibonacci_01 generator and calls @c seed(gen). */
276 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(lagged_fibonacci_01_engine, SeedSeq, seq)
278 template<class It> lagged_fibonacci_01_engine(It& first, It last)
279 { seed(first, last); }
281 // compiler-generated copy ctor and assignment operator are fine
283 /** Calls seed(default_seed). */
284 void seed() { seed(default_seed); }
287 * Constructs a \minstd_rand0 generator with the constructor parameter
288 * value and calls seed with it. Distinct seeds in the range
289 * [1, 2147483647) will produce generators with different states. Other
290 * seeds will be equivalent to some seed within this range. See
291 * \linear_congruential_engine for details.
293 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(lagged_fibonacci_01_engine, boost::uint32_t, value)
295 minstd_rand0 intgen(value);
296 detail::generator_seed_seq<minstd_rand0> gen(intgen);
301 * Seeds this @c lagged_fibonacci_01_engine using values produced by
304 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(lagged_fibonacci_01_engine, SeedSeq, seq)
306 detail::seed_array_real<w>(seq, x);
311 * Seeds this @c lagged_fibonacci_01_engine using values from the
312 * iterator range [first, last). If there are not enough elements
313 * in the range, throws @c std::invalid_argument.
316 void seed(It& first, It last)
318 detail::fill_array_real<w>(first, last, x);
322 /** Returns the smallest value that the generator can produce. */
323 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return result_type(0); }
324 /** Returns the upper bound of the generators outputs. */
325 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () { return result_type(1); }
327 /** Returns the next value of the generator. */
328 result_type operator()()
335 /** Fills a range with random values */
337 void generate(Iter first, Iter last)
338 { return detail::generate_from_real(*this, first, last); }
340 /** Advances the state of the generator by @c z. */
341 void discard(boost::uintmax_t z)
343 for(boost::uintmax_t j = 0; j < z; ++j) {
349 * Writes the textual representation of the generator to a @c std::ostream.
351 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, lagged_fibonacci_01_engine, f)
353 // allow for Koenig lookup
356 std::ios_base::fmtflags oldflags = os.flags(os.dec | os.fixed | os.left);
357 for(unsigned int i = 0; i < f.long_lag; ++i)
358 os << ' ' << f.x[i] * f.modulus();
364 * Reads the textual representation of the generator from a @c std::istream.
366 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, lagged_fibonacci_01_engine, f)
369 for(unsigned int i = 0; i < f.long_lag; ++i) {
370 typename lagged_fibonacci_01_engine::result_type value;
371 is >> std::ws >> value;
372 f.x[i] = value / f.modulus();
378 * Returns true if the two generators will produce identical
379 * sequences of outputs.
381 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(lagged_fibonacci_01_engine, x, y)
382 { return x.i == y.i && std::equal(x.x, x.x+long_lag, y.x); }
385 * Returns true if the two generators will produce different
386 * sequences of outputs.
388 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(lagged_fibonacci_01_engine)
391 /// \cond show_private
393 static RealType modulus()
396 return pow(RealType(2), word_size);
400 RealType x[long_lag];
403 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
404 // A definition is required even for integral static constants
405 template<class RealType, int w, unsigned int p, unsigned int q>
406 const bool lagged_fibonacci_01_engine<RealType, w, p, q>::has_fixed_range;
407 template<class RealType, int w, unsigned int p, unsigned int q>
408 const unsigned int lagged_fibonacci_01_engine<RealType, w, p, q>::long_lag;
409 template<class RealType, int w, unsigned int p, unsigned int q>
410 const unsigned int lagged_fibonacci_01_engine<RealType, w, p, q>::short_lag;
411 template<class RealType, int w, unsigned int p, unsigned int q>
412 const int lagged_fibonacci_01_engine<RealType,w,p,q>::word_size;
413 template<class RealType, int w, unsigned int p, unsigned int q>
414 const boost::uint32_t lagged_fibonacci_01_engine<RealType,w,p,q>::default_seed;
417 /// \cond show_private
418 template<class RealType, int w, unsigned int p, unsigned int q>
419 void lagged_fibonacci_01_engine<RealType, w, p, q>::fill()
421 // two loops to avoid costly modulo operations
422 { // extra scope for MSVC brokenness w.r.t. for scope
423 for(unsigned int j = 0; j < short_lag; ++j) {
424 RealType t = x[j] + x[j+(long_lag-short_lag)];
430 for(unsigned int j = short_lag; j < long_lag; ++j) {
431 RealType t = x[j] + x[j-short_lag];
440 /// \cond show_deprecated
442 // provided for backwards compatibility
443 template<class RealType, int w, unsigned int p, unsigned int q>
444 class lagged_fibonacci_01 : public lagged_fibonacci_01_engine<RealType, w, p, q>
446 typedef lagged_fibonacci_01_engine<RealType, w, p, q> base_type;
448 lagged_fibonacci_01() {}
449 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(lagged_fibonacci_01, boost::uint32_t, val)
451 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(lagged_fibonacci_01, SeedSeq, seq)
454 lagged_fibonacci_01(It& first, It last) : base_type(first, last) {}
461 template<class Engine>
462 struct generator_bits;
464 template<class RealType, int w, unsigned int p, unsigned int q>
465 struct generator_bits<lagged_fibonacci_01_engine<RealType, w, p, q> >
467 static std::size_t value() { return w; }
470 template<class RealType, int w, unsigned int p, unsigned int q>
471 struct generator_bits<lagged_fibonacci_01<RealType, w, p, q> >
473 static std::size_t value() { return w; }
478 #ifdef BOOST_RANDOM_DOXYGEN
481 * The specializations lagged_fibonacci607 ... lagged_fibonacci44497
482 * use well tested lags.
487 * "On the Periods of Generalized Fibonacci Recurrences", Richard P. Brent
488 * Computer Sciences Laboratory Australian National University, December 1992
491 * The lags used here can be found in
494 * "Uniform random number generators for supercomputers", Richard Brent,
495 * Proc. of Fifth Australian Supercomputer Conference, Melbourne,
496 * Dec. 1992, pp. 704-706.
499 struct lagged_fibonacci_doc {};
503 /** @copydoc boost::random::detail::lagged_fibonacci_doc */
504 typedef lagged_fibonacci_01_engine<double, 48, 607, 273> lagged_fibonacci607;
505 /** @copydoc boost::random::detail::lagged_fibonacci_doc */
506 typedef lagged_fibonacci_01_engine<double, 48, 1279, 418> lagged_fibonacci1279;
507 /** @copydoc boost::random::detail::lagged_fibonacci_doc */
508 typedef lagged_fibonacci_01_engine<double, 48, 2281, 1252> lagged_fibonacci2281;
509 /** @copydoc boost::random::detail::lagged_fibonacci_doc */
510 typedef lagged_fibonacci_01_engine<double, 48, 3217, 576> lagged_fibonacci3217;
511 /** @copydoc boost::random::detail::lagged_fibonacci_doc */
512 typedef lagged_fibonacci_01_engine<double, 48, 4423, 2098> lagged_fibonacci4423;
513 /** @copydoc boost::random::detail::lagged_fibonacci_doc */
514 typedef lagged_fibonacci_01_engine<double, 48, 9689, 5502> lagged_fibonacci9689;
515 /** @copydoc boost::random::detail::lagged_fibonacci_doc */
516 typedef lagged_fibonacci_01_engine<double, 48, 19937, 9842> lagged_fibonacci19937;
517 /** @copydoc boost::random::detail::lagged_fibonacci_doc */
518 typedef lagged_fibonacci_01_engine<double, 48, 23209, 13470> lagged_fibonacci23209;
519 /** @copydoc boost::random::detail::lagged_fibonacci_doc */
520 typedef lagged_fibonacci_01_engine<double, 48, 44497, 21034> lagged_fibonacci44497;
522 } // namespace random
524 using random::lagged_fibonacci607;
525 using random::lagged_fibonacci1279;
526 using random::lagged_fibonacci2281;
527 using random::lagged_fibonacci3217;
528 using random::lagged_fibonacci4423;
529 using random::lagged_fibonacci9689;
530 using random::lagged_fibonacci19937;
531 using random::lagged_fibonacci23209;
532 using random::lagged_fibonacci44497;
536 #endif // BOOST_RANDOM_LAGGED_FIBONACCI_HPP