1 /* boost random/linear_congruential.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: linear_congruential.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP
17 #define BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP
22 #include <boost/config.hpp>
23 #include <boost/limits.hpp>
24 #include <boost/static_assert.hpp>
25 #include <boost/random/detail/config.hpp>
26 #include <boost/random/detail/const_mod.hpp>
27 #include <boost/detail/workaround.hpp>
29 #include <boost/random/detail/disable_warnings.hpp>
35 * Instantiations of class template linear_congruential model a
36 * \pseudo_random_number_generator. Linear congruential pseudo-random
37 * number generators are described in:
39 * "Mathematical methods in large-scale computing units", D. H. Lehmer,
40 * Proc. 2nd Symposium on Large-Scale Digital Calculating Machines,
41 * Harvard University Press, 1951, pp. 141-146
43 * Let x(n) denote the sequence of numbers returned by some pseudo-random
44 * number generator. Then for the linear congruential generator,
45 * x(n+1) := (a * x(n) + c) mod m. Parameters for the generator are
46 * x(0), a, c, m. The template parameter IntType shall denote an integral
47 * type. It must be large enough to hold values a, c, and m. The template
48 * parameters a and c must be smaller than m.
50 * Note: The quality of the generator crucially depends on the choice of
51 * the parameters. User code should use one of the sensibly parameterized
52 * generators such as minstd_rand instead.
54 template<class IntType, IntType a, IntType c, IntType m, IntType val>
55 class linear_congruential
58 typedef IntType result_type;
59 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
60 static const bool has_fixed_range = true;
61 static const result_type min_value = ( c == 0 ? 1 : 0 );
62 static const result_type max_value = m-1;
64 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
66 BOOST_STATIC_CONSTANT(IntType, multiplier = a);
67 BOOST_STATIC_CONSTANT(IntType, increment = c);
68 BOOST_STATIC_CONSTANT(IntType, modulus = m);
70 // MSVC 6 and possibly others crash when encountering complicated integral
71 // constant expressions. Avoid the check for now.
72 // BOOST_STATIC_ASSERT(m == 0 || a < m);
73 // BOOST_STATIC_ASSERT(m == 0 || c < m);
76 * Constructs a linear_congruential generator, seeding it with @c x0.
78 explicit linear_congruential(IntType x0 = 1)
82 // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
83 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
84 BOOST_STATIC_ASSERT(std::numeric_limits<IntType>::is_integer);
89 * Constructs a @c linear_congruential generator and seeds it
90 * with values taken from the itrator range [first, last)
91 * and adjusts first to point to the element after the last one
92 * used. If there are not enough elements, throws @c std::invalid_argument.
94 * first and last must be input iterators.
97 linear_congruential(It& first, It last)
102 // compiler-generated copy constructor and assignment operator are fine
105 * If c mod m is zero and x0 mod m is zero, changes the current value of
106 * the generator to 1. Otherwise, changes it to x0 mod m. If c is zero,
107 * distinct seeds in the range [1,m) will leave the generator in distinct
108 * states. If c is not zero, the range is [0,m).
110 void seed(IntType x0 = 1)
112 // wrap _x if it doesn't fit in the destination
118 // handle negative seeds
119 if(_x <= 0 && _x != 0) {
122 // adjust to the correct range
123 if(increment == 0 && _x == 0) {
126 assert(_x >= (min)());
127 assert(_x <= (max)());
131 * seeds a @c linear_congruential generator with values taken
132 * from the itrator range [first, last) and adjusts @c first to
133 * point to the element after the last one used. If there are
134 * not enough elements, throws @c std::invalid_argument.
136 * @c first and @c last must be input iterators.
139 void seed(It& first, It last)
142 throw std::invalid_argument("linear_congruential::seed");
147 * Returns the smallest value that the @c linear_congruential generator
150 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return c == 0 ? 1 : 0; }
152 * Returns the largest value that the @c linear_congruential generator
155 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return modulus-1; }
157 /** Returns the next value of the @c linear_congruential generator. */
160 _x = const_mod<IntType, m>::mult_add(a, _x, c);
164 static bool validation(IntType x) { return val == x; }
166 #ifdef BOOST_NO_OPERATORS_IN_NAMESPACE
168 // Use a member function; Streamable concept not supported.
169 bool operator==(const linear_congruential& rhs) const
170 { return _x == rhs._x; }
171 bool operator!=(const linear_congruential& rhs) const
172 { return !(*this == rhs); }
175 friend bool operator==(const linear_congruential& x,
176 const linear_congruential& y)
177 { return x._x == y._x; }
178 friend bool operator!=(const linear_congruential& x,
179 const linear_congruential& y)
180 { return !(x == y); }
182 #if !defined(BOOST_RANDOM_NO_STREAM_OPERATORS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
183 template<class CharT, class Traits>
184 friend std::basic_ostream<CharT,Traits>&
185 operator<<(std::basic_ostream<CharT,Traits>& os,
186 const linear_congruential& lcg)
191 template<class CharT, class Traits>
192 friend std::basic_istream<CharT,Traits>&
193 operator>>(std::basic_istream<CharT,Traits>& is,
194 linear_congruential& lcg)
206 // probably needs the "no native streams" caveat for STLPort
207 #if !defined(__SGI_STL_PORT) && BOOST_WORKAROUND(__GNUC__, == 2)
208 template<class IntType, IntType a, IntType c, IntType m, IntType val>
210 operator<<(std::ostream& os,
211 const linear_congruential<IntType,a,c,m,val>& lcg)
216 template<class IntType, IntType a, IntType c, IntType m, IntType val>
218 operator>>(std::istream& is,
219 linear_congruential<IntType,a,c,m,val>& lcg)
223 #elif defined(BOOST_RANDOM_NO_STREAM_OPERATORS) || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
224 template<class CharT, class Traits, class IntType, IntType a, IntType c, IntType m, IntType val>
225 std::basic_ostream<CharT,Traits>&
226 operator<<(std::basic_ostream<CharT,Traits>& os,
227 const linear_congruential<IntType,a,c,m,val>& lcg)
232 template<class CharT, class Traits, class IntType, IntType a, IntType c, IntType m, IntType val>
233 std::basic_istream<CharT,Traits>&
234 operator>>(std::basic_istream<CharT,Traits>& is,
235 linear_congruential<IntType,a,c,m,val>& lcg)
241 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
242 // A definition is required even for integral static constants
243 template<class IntType, IntType a, IntType c, IntType m, IntType val>
244 const bool linear_congruential<IntType, a, c, m, val>::has_fixed_range;
245 template<class IntType, IntType a, IntType c, IntType m, IntType val>
246 const typename linear_congruential<IntType, a, c, m, val>::result_type linear_congruential<IntType, a, c, m, val>::min_value;
247 template<class IntType, IntType a, IntType c, IntType m, IntType val>
248 const typename linear_congruential<IntType, a, c, m, val>::result_type linear_congruential<IntType, a, c, m, val>::max_value;
249 template<class IntType, IntType a, IntType c, IntType m, IntType val>
250 const IntType linear_congruential<IntType,a,c,m,val>::modulus;
253 } // namespace random
255 // validation values from the publications
257 * The specialization \minstd_rand0 was originally suggested in
260 * A pseudo-random number generator for the System/360, P.A. Lewis,
261 * A.S. Goodman, J.M. Miller, IBM Systems Journal, Vol. 8, No. 2,
265 * It is examined more closely together with \minstd_rand in
268 * "Random Number Generators: Good ones are hard to find",
269 * Stephen K. Park and Keith W. Miller, Communications of
270 * the ACM, Vol. 31, No. 10, October 1988, pp. 1192-1201
273 typedef random::linear_congruential<int32_t, 16807, 0, 2147483647,
274 1043618065> minstd_rand0;
276 /** The specialization \minstd_rand was suggested in
279 * "Random Number Generators: Good ones are hard to find",
280 * Stephen K. Park and Keith W. Miller, Communications of
281 * the ACM, Vol. 31, No. 10, October 1988, pp. 1192-1201
284 typedef random::linear_congruential<int32_t, 48271, 0, 2147483647,
285 399268537> minstd_rand;
288 #if !defined(BOOST_NO_INT64_T) && !defined(BOOST_NO_INTEGRAL_INT64_T)
289 /** Class @c rand48 models a \pseudo_random_number_generator. It uses
290 * the linear congruential algorithm with the parameters a = 0x5DEECE66D,
291 * c = 0xB, m = 2**48. It delivers identical results to the @c lrand48()
292 * function available on some systems (assuming lcong48 has not been called).
294 * It is only available on systems where @c uint64_t is provided as an
295 * integral type, so that for example static in-class constants and/or
296 * enum definitions with large @c uint64_t numbers work.
301 typedef int32_t result_type;
302 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
303 static const bool has_fixed_range = true;
304 static const int32_t min_value = 0;
305 static const int32_t max_value = integer_traits<int32_t>::const_max;
307 enum { has_fixed_range = false };
310 * Returns the smallest value that the generator can produce
312 int32_t min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
314 * Returns the largest value that the generator can produce
316 int32_t max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return std::numeric_limits<int32_t>::max BOOST_PREVENT_MACRO_SUBSTITUTION (); }
318 #ifdef BOOST_RANDOM_DOXYGEN
320 * If T is an integral type smaller than int46_t, constructs
321 * a \rand48 generator with x(0) := (x0 << 16) | 0x330e. Otherwise
322 * constructs a \rand48 generator with x(0) = x0.
324 template<class T> explicit rand48(T x0 = 1);
326 rand48() : lcf(cnv(static_cast<int32_t>(1))) {}
327 template<class T> explicit rand48(T x0) : lcf(cnv(x0)) { }
329 template<class It> rand48(It& first, It last) : lcf(first, last) { }
331 // compiler-generated copy ctor and assignment operator are fine
333 #ifdef BOOST_RANDOM_DOXYGEN
335 * If T is an integral type smaller than int46_t, changes
336 * the current value x(n) of the generator to (x0 << 16) | 0x330e.
337 * Otherwise changes the current value x(n) to x0.
339 template<class T> void seed(T x0 = 1);
341 void seed() { seed(static_cast<int32_t>(1)); }
342 template<class T> void seed(T x0) { lcf.seed(cnv(x0)); }
344 template<class It> void seed(It& first, It last) { lcf.seed(first,last); }
347 * Returns the next value of the generator.
349 int32_t operator()() { return static_cast<int32_t>(lcf() >> 17); }
350 // by experiment from lrand48()
351 static bool validation(int32_t x) { return x == 1993516219; }
353 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
355 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
356 template<class CharT,class Traits>
357 friend std::basic_ostream<CharT,Traits>&
358 operator<<(std::basic_ostream<CharT,Traits>& os, const rand48& r)
359 { os << r.lcf; return os; }
361 template<class CharT,class Traits>
362 friend std::basic_istream<CharT,Traits>&
363 operator>>(std::basic_istream<CharT,Traits>& is, rand48& r)
364 { is >> r.lcf; return is; }
367 friend bool operator==(const rand48& x, const rand48& y)
368 { return x.lcf == y.lcf; }
369 friend bool operator!=(const rand48& x, const rand48& y)
370 { return !(x == y); }
372 // Use a member function; Streamable concept not supported.
373 bool operator==(const rand48& rhs) const
374 { return lcf == rhs.lcf; }
375 bool operator!=(const rand48& rhs) const
376 { return !(*this == rhs); }
379 /// \cond hide_private_members
380 random::linear_congruential<uint64_t,
381 uint64_t(0xDEECE66DUL) | (uint64_t(0x5) << 32), // xxxxULL is not portable
382 0xB, uint64_t(1)<<48, /* unknown */ 0> lcf;
384 static uint64_t cnv(T x)
386 if(sizeof(T) < sizeof(uint64_t)) {
387 return (static_cast<uint64_t>(x) << 16) | 0x330e;
389 return(static_cast<uint64_t>(x));
392 static uint64_t cnv(float x) { return(static_cast<uint64_t>(x)); }
393 static uint64_t cnv(double x) { return(static_cast<uint64_t>(x)); }
394 static uint64_t cnv(long double x) { return(static_cast<uint64_t>(x)); }
397 #endif /* !BOOST_NO_INT64_T && !BOOST_NO_INTEGRAL_INT64_T */
401 #include <boost/random/detail/enable_warnings.hpp>
403 #endif // BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP