1
/* boost random/lagged_fibonacci.hpp header file
3
* Copyright Jens Maurer 2000-2001
4
* Permission to use, copy, modify, sell, and distribute this software
5
* is hereby granted without fee provided that the above copyright notice
6
* appears in all copies and that both that copyright notice and this
7
* permission notice appear in supporting documentation,
9
* Jens Maurer makes no representations about the suitability of this
10
* software for any purpose. It is provided "as is" without express or
13
* See http://www.boost.org for most recent version including documentation.
15
* $Id: lagged_fibonacci.hpp,v 1.1 2004/02/27 03:16:46 pseudonym Exp $
18
* 2001-02-18 moved to individual header files
21
#ifndef BOOST_RANDOM_LAGGED_FIBONACCI_HPP
22
#define BOOST_RANDOM_LAGGED_FIBONACCI_HPP
25
#include <algorithm> // std::max
27
#include <boost/config.hpp>
28
#include <boost/limits.hpp>
29
#include <boost/cstdint.hpp>
30
#include <boost/random/linear_congruential.hpp>
31
#include <boost/random/uniform_01.hpp>
36
# if BOOST_WORKAROUND(_MSC_FULL_VER, BOOST_TESTED_AT(13102292) && BOOST_MSVC > 1300)
39
template<class IStream, class F, class RealType>
41
extract_lagged_fibonacci_01(
49
for(unsigned int i = 0; i < f.long_lag; ++i)
52
is >> value >> std::ws;
53
x[i] = value / modulus;
58
template<class IStream, class F, class UIntType>
60
extract_lagged_fibonacci(
67
for(unsigned int i = 0; i < f.long_lag; ++i)
68
is >> x[i] >> std::ws;
74
template<class UIntType, int w, unsigned int p, unsigned int q,
76
class lagged_fibonacci
79
typedef UIntType result_type;
80
BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
81
BOOST_STATIC_CONSTANT(int, word_size = w);
82
BOOST_STATIC_CONSTANT(unsigned int, long_lag = p);
83
BOOST_STATIC_CONSTANT(unsigned int, short_lag = q);
85
result_type min() const { return 0; }
86
result_type max() const { return wordmask; }
88
lagged_fibonacci() { init_wordmask(); seed(); }
89
explicit lagged_fibonacci(uint32_t value) { init_wordmask(); seed(value); }
90
template<class It> lagged_fibonacci(It& first, It last)
91
{ init_wordmask(); seed(first, last); }
92
// compiler-generated copy ctor and assignment operator are fine
98
for(int i = 0; i < w; ++i)
99
wordmask |= (1u << i);
103
void seed(uint32_t value = 331u)
105
minstd_rand0 gen(value);
106
for(unsigned int j = 0; j < long_lag; ++j)
107
x[j] = gen() & wordmask;
112
void seed(It& first, It last)
114
// word size could be smaller than the seed values
116
for(j = 0; j < long_lag && first != last; ++j, ++first)
117
x[j] = *first & wordmask;
119
if(first == last && j < long_lag)
120
throw std::invalid_argument("lagged_fibonacci::seed");
123
result_type operator()()
130
static bool validation(result_type x)
135
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
137
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
138
template<class CharT, class Traits>
139
friend std::basic_ostream<CharT,Traits>&
140
operator<<(std::basic_ostream<CharT,Traits>& os, const lagged_fibonacci& f)
143
for(unsigned int i = 0; i < f.long_lag; ++i)
148
template<class CharT, class Traits>
149
friend std::basic_istream<CharT, Traits>&
150
operator>>(std::basic_istream<CharT, Traits>& is, lagged_fibonacci& f)
152
# if BOOST_WORKAROUND(_MSC_FULL_VER, BOOST_TESTED_AT(13102292)) && BOOST_MSVC > 1300
153
return detail::extract_lagged_fibonacci(is, f, f.i, f.x);
155
is >> f.i >> std::ws;
156
for(unsigned int i = 0; i < f.long_lag; ++i)
157
is >> f.x[i] >> std::ws;
163
friend bool operator==(const lagged_fibonacci& x, const lagged_fibonacci& y)
164
{ return x.i == y.i && std::equal(x.x, x.x+long_lag, y.x); }
165
friend bool operator!=(const lagged_fibonacci& x,
166
const lagged_fibonacci& y)
167
{ return !(x == y); }
169
// Use a member function; Streamable concept not supported.
170
bool operator==(const lagged_fibonacci& rhs) const
171
{ return i == rhs.i && std::equal(x, x+long_lag, rhs.x); }
172
bool operator!=(const lagged_fibonacci& rhs) const
173
{ return !(*this == rhs); }
180
UIntType x[long_lag];
183
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
184
// A definition is required even for integral static constants
185
template<class UIntType, int w, unsigned int p, unsigned int q, UIntType val>
186
const bool lagged_fibonacci<UIntType, w, p, q, val>::has_fixed_range;
187
template<class UIntType, int w, unsigned int p, unsigned int q, UIntType val>
188
const unsigned int lagged_fibonacci<UIntType, w, p, q, val>::long_lag;
189
template<class UIntType, int w, unsigned int p, unsigned int q, UIntType val>
190
const unsigned int lagged_fibonacci<UIntType, w, p, q, val>::short_lag;
193
template<class UIntType, int w, unsigned int p, unsigned int q, UIntType val>
194
void lagged_fibonacci<UIntType, w, p, q, val>::fill()
196
// two loops to avoid costly modulo operations
197
{ // extra scope for MSVC brokenness w.r.t. for scope
198
for(unsigned int j = 0; j < short_lag; ++j)
199
x[j] = (x[j] + x[j+(long_lag-short_lag)]) & wordmask;
201
for(unsigned int j = short_lag; j < long_lag; ++j)
202
x[j] = (x[j] + x[j-short_lag]) & wordmask;
208
// lagged Fibonacci generator for the range [0..1)
209
// contributed by Matthias Troyer
210
// for p=55, q=24 originally by G. J. Mitchell and D. P. Moore 1958
212
template<class T, unsigned int p, unsigned int q>
213
struct fibonacci_validation
215
BOOST_STATIC_CONSTANT(bool, is_specialized = false);
216
static T value() { return 0; }
217
static T tolerance() { return 0; }
220
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
221
// A definition is required even for integral static constants
222
template<class T, unsigned int p, unsigned int q>
223
const bool fibonacci_validation<T, p, q>::is_specialized;
226
#define BOOST_RANDOM_FIBONACCI_VAL(T,P,Q,V,E) \
228
struct fibonacci_validation<T, P, Q> \
230
BOOST_STATIC_CONSTANT(bool, is_specialized = true); \
231
static T value() { return V; } \
232
static T tolerance() \
233
{ return std::max(E, static_cast<T>(5*std::numeric_limits<T>::epsilon())); } \
235
// (The extra static_cast<T> in the std::max call above is actually
236
// unnecessary except for HP aCC 1.30, which claims that
237
// numeric_limits<double>::epsilon() doesn't actually return a double.)
239
BOOST_RANDOM_FIBONACCI_VAL(double, 607, 273, 0.4293817707235914, 1e-14)
240
BOOST_RANDOM_FIBONACCI_VAL(double, 1279, 418, 0.9421630240437659, 1e-14)
241
BOOST_RANDOM_FIBONACCI_VAL(double, 2281, 1252, 0.1768114046909004, 1e-14)
242
BOOST_RANDOM_FIBONACCI_VAL(double, 3217, 576, 0.1956232694868209, 1e-14)
243
BOOST_RANDOM_FIBONACCI_VAL(double, 4423, 2098, 0.9499762202147172, 1e-14)
244
BOOST_RANDOM_FIBONACCI_VAL(double, 9689, 5502, 0.05737836943695162, 1e-14)
245
BOOST_RANDOM_FIBONACCI_VAL(double, 19937, 9842, 0.5076528587449834, 1e-14)
246
BOOST_RANDOM_FIBONACCI_VAL(double, 23209, 13470, 0.5414473810619185, 1e-14)
247
BOOST_RANDOM_FIBONACCI_VAL(double, 44497,21034, 0.254135073399297, 1e-14)
249
#undef BOOST_RANDOM_FIBONACCI_VAL
251
template<class RealType, int w, unsigned int p, unsigned int q>
252
class lagged_fibonacci_01
255
typedef RealType result_type;
256
BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
257
BOOST_STATIC_CONSTANT(int, word_size = w);
258
BOOST_STATIC_CONSTANT(unsigned int, long_lag = p);
259
BOOST_STATIC_CONSTANT(unsigned int, short_lag = q);
261
lagged_fibonacci_01() { init_modulus(); seed(); }
262
explicit lagged_fibonacci_01(uint32_t value) { init_modulus(); seed(value); }
263
template<class Generator>
264
explicit lagged_fibonacci_01(Generator & gen) { init_modulus(); seed(gen); }
265
template<class It> lagged_fibonacci_01(It& first, It last)
266
{ init_modulus(); seed(first, last); }
267
// compiler-generated copy ctor and assignment operator are fine
272
#ifndef BOOST_NO_STDC_NAMESPACE
273
// allow for Koenig lookup
276
_modulus = pow(RealType(2), word_size);
280
void seed(uint32_t value = 331u)
282
minstd_rand0 intgen(value);
286
// For GCC, moving this function out-of-line prevents inlining, which may
287
// reduce overall object code size. However, MSVC does not grok
288
// out-of-line template member functions.
289
template<class Generator>
290
void seed(Generator & gen)
292
uniform_01<Generator, RealType> gen01(gen);
293
// I could have used std::generate_n, but it takes "gen" by value
294
for(unsigned int j = 0; j < long_lag; ++j)
300
void seed(It& first, It last)
302
#ifndef BOOST_NO_STDC_NAMESPACE
303
// allow for Koenig lookup
307
unsigned long mask = ~((~0u) << (w%32)); // now lowest w bits set
308
RealType two32 = pow(RealType(2), 32);
310
for(j = 0; j < long_lag && first != last; ++j, ++first) {
312
for(int i = 0; i < w/32 && first != last; ++i, ++first)
313
x[j] += *first / pow(two32,i+1);
314
if(first != last && mask != 0)
315
x[j] += fmod((*first & mask) / _modulus, RealType(1));
318
if(first == last && j < long_lag)
319
throw std::invalid_argument("lagged_fibonacci_01::seed");
322
result_type min() const { return result_type(0); }
323
result_type max() const { return result_type(1); }
325
result_type operator()()
332
static bool validation(result_type x)
334
result_type v = fibonacci_validation<result_type, p, q>::value();
335
result_type epsilon = fibonacci_validation<result_type, p, q>::tolerance();
336
// std::abs is a source of trouble: sometimes, it's not overloaded
337
// for double, plus the usual namespace std noncompliance -> avoid it
339
// return abs(x - v) < 5 * epsilon
340
return x > v - epsilon && x < v + epsilon;
343
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
345
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
346
template<class CharT, class Traits>
347
friend std::basic_ostream<CharT,Traits>&
348
operator<<(std::basic_ostream<CharT,Traits>& os, const lagged_fibonacci_01&f)
350
#ifndef BOOST_NO_STDC_NAMESPACE
351
// allow for Koenig lookup
355
std::ios_base::fmtflags oldflags = os.flags(os.dec | os.fixed | os.left);
356
for(unsigned int i = 0; i < f.long_lag; ++i)
357
os << f.x[i] * f._modulus << " ";
362
template<class CharT, class Traits>
363
friend std::basic_istream<CharT, Traits>&
364
operator>>(std::basic_istream<CharT, Traits>& is, lagged_fibonacci_01& f)
366
# if BOOST_WORKAROUND(_MSC_FULL_VER, BOOST_TESTED_AT(13102292)) && BOOST_MSVC > 1300
367
return detail::extract_lagged_fibonacci_01(is, f, f.i, f.x, f._modulus);
369
is >> f.i >> std::ws;
370
for(unsigned int i = 0; i < f.long_lag; ++i) {
372
is >> value >> std::ws;
373
f.x[i] = value / f._modulus;
380
friend bool operator==(const lagged_fibonacci_01& x,
381
const lagged_fibonacci_01& y)
382
{ return x.i == y.i && std::equal(x.x, x.x+long_lag, y.x); }
383
friend bool operator!=(const lagged_fibonacci_01& x,
384
const lagged_fibonacci_01& y)
385
{ return !(x == y); }
387
// Use a member function; Streamable concept not supported.
388
bool operator==(const lagged_fibonacci_01& rhs) const
389
{ return i == rhs.i && std::equal(x, x+long_lag, rhs.x); }
390
bool operator!=(const lagged_fibonacci_01& rhs) const
391
{ return !(*this == rhs); }
397
RealType x[long_lag];
401
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
402
// A definition is required even for integral static constants
403
template<class RealType, int w, unsigned int p, unsigned int q>
404
const bool lagged_fibonacci_01<RealType, w, p, q>::has_fixed_range;
405
template<class RealType, int w, unsigned int p, unsigned int q>
406
const unsigned int lagged_fibonacci_01<RealType, w, p, q>::long_lag;
407
template<class RealType, int w, unsigned int p, unsigned int q>
408
const unsigned int lagged_fibonacci_01<RealType, w, p, q>::short_lag;
412
template<class RealType, int w, unsigned int p, unsigned int q>
413
void lagged_fibonacci_01<RealType, w, p, q>::fill()
415
// two loops to avoid costly modulo operations
416
{ // extra scope for MSVC brokenness w.r.t. for scope
417
for(unsigned int j = 0; j < short_lag; ++j) {
418
RealType t = x[j] + x[j+(long_lag-short_lag)];
424
for(unsigned int j = short_lag; j < long_lag; ++j) {
425
RealType t = x[j] + x[j-short_lag];
433
} // namespace random
435
typedef random::lagged_fibonacci_01<double, 48, 607, 273> lagged_fibonacci607;
436
typedef random::lagged_fibonacci_01<double, 48, 1279, 418> lagged_fibonacci1279;
437
typedef random::lagged_fibonacci_01<double, 48, 2281, 1252> lagged_fibonacci2281;
438
typedef random::lagged_fibonacci_01<double, 48, 3217, 576> lagged_fibonacci3217;
439
typedef random::lagged_fibonacci_01<double, 48, 4423, 2098> lagged_fibonacci4423;
440
typedef random::lagged_fibonacci_01<double, 48, 9689, 5502> lagged_fibonacci9689;
441
typedef random::lagged_fibonacci_01<double, 48, 19937, 9842> lagged_fibonacci19937;
442
typedef random::lagged_fibonacci_01<double, 48, 23209, 13470> lagged_fibonacci23209;
443
typedef random::lagged_fibonacci_01<double, 48, 44497, 21034> lagged_fibonacci44497;
446
// It is possible to partially specialize uniform_01<> on lagged_fibonacci_01<>
447
// to help the compiler generate efficient code. For GCC, this seems useless,
448
// because GCC optimizes (x-0)/(1-0) to (x-0). This is good enough for now.
452
#endif // BOOST_RANDOM_LAGGED_FIBONACCI_HPP