williamr@2: /* boost random/linear_congruential.hpp header file
williamr@2:  *
williamr@2:  * Copyright Jens Maurer 2000-2001
williamr@2:  * Distributed under the Boost Software License, Version 1.0. (See
williamr@2:  * accompanying file LICENSE_1_0.txt or copy at
williamr@2:  * http://www.boost.org/LICENSE_1_0.txt)
williamr@2:  *
williamr@2:  * See http://www.boost.org for most recent version including documentation.
williamr@2:  *
williamr@2:  * $Id: linear_congruential.hpp,v 1.22 2005/05/21 15:57:00 dgregor Exp $
williamr@2:  *
williamr@2:  * Revision history
williamr@2:  *  2001-02-18  moved to individual header files
williamr@2:  */
williamr@2: 
williamr@2: #ifndef BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP
williamr@2: #define BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP
williamr@2: 
williamr@2: #include <iostream>
williamr@2: #include <cassert>
williamr@2: #include <stdexcept>
williamr@2: #include <boost/config.hpp>
williamr@2: #include <boost/limits.hpp>
williamr@2: #include <boost/static_assert.hpp>
williamr@2: #include <boost/random/detail/const_mod.hpp>
williamr@2: #include <boost/detail/workaround.hpp>
williamr@2: 
williamr@2: namespace boost {
williamr@2: namespace random {
williamr@2: 
williamr@2: // compile-time configurable linear congruential generator
williamr@2: template<class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2: class linear_congruential
williamr@2: {
williamr@2: public:
williamr@2:   typedef IntType result_type;
williamr@2: #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
williamr@2:   static const bool has_fixed_range = true;
williamr@2:   static const result_type min_value = ( c == 0 ? 1 : 0 );
williamr@2:   static const result_type max_value = m-1;
williamr@2: #else
williamr@2:   BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
williamr@2: #endif
williamr@2:   BOOST_STATIC_CONSTANT(IntType, multiplier = a);
williamr@2:   BOOST_STATIC_CONSTANT(IntType, increment = c);
williamr@2:   BOOST_STATIC_CONSTANT(IntType, modulus = m);
williamr@2: 
williamr@2:   // MSVC 6 and possibly others crash when encountering complicated integral
williamr@2:   // constant expressions.  Avoid the check for now.
williamr@2:   // BOOST_STATIC_ASSERT(m == 0 || a < m);
williamr@2:   // BOOST_STATIC_ASSERT(m == 0 || c < m);
williamr@2: 
williamr@2:   explicit linear_congruential(IntType x0 = 1)
williamr@2:     : _modulus(modulus), _x(_modulus ? (x0 % _modulus) : x0)
williamr@2:   { 
williamr@2:     assert(c || x0); /* if c == 0 and x(0) == 0 then x(n) = 0 for all n */
williamr@2:     // overflow check
williamr@2:     // disabled because it gives spurious "divide by zero" gcc warnings
williamr@2:     // assert(m == 0 || (a*(m-1)+c) % m == (c < a ? c-a+m : c-a)); 
williamr@2: 
williamr@2:     // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
williamr@2: #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
williamr@2:     BOOST_STATIC_ASSERT(std::numeric_limits<IntType>::is_integer);
williamr@2: #endif
williamr@2:   }
williamr@2: 
williamr@2:   template<class It>
williamr@2:   linear_congruential(It& first, It last) { seed(first, last); }
williamr@2: 
williamr@2:   // compiler-generated copy constructor and assignment operator are fine
williamr@2:   void seed(IntType x0 = 1)
williamr@2:   {
williamr@2:     assert(c || x0);
williamr@2:     _x = (_modulus ? (x0 % _modulus) : x0);
williamr@2:   }
williamr@2: 
williamr@2:   template<class It>
williamr@2:   void seed(It& first, It last)
williamr@2:   {
williamr@2:     if(first == last)
williamr@2:       throw std::invalid_argument("linear_congruential::seed");
williamr@2:     IntType value = *first++;
williamr@2:     _x = (_modulus ? (value % _modulus) : value);
williamr@2:   }
williamr@2: 
williamr@2:   result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return c == 0 ? 1 : 0; }
williamr@2:   result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return modulus-1; }
williamr@2: 
williamr@2:   IntType operator()()
williamr@2:   {
williamr@2:     _x = const_mod<IntType, m>::mult_add(a, _x, c);
williamr@2:     return _x;
williamr@2:   }
williamr@2: 
williamr@2:   static bool validation(IntType x) { return val == x; }
williamr@2: 
williamr@2: #ifdef BOOST_NO_OPERATORS_IN_NAMESPACE
williamr@2:     
williamr@2:   // Use a member function; Streamable concept not supported.
williamr@2:   bool operator==(const linear_congruential& rhs) const
williamr@2:   { return _x == rhs._x; }
williamr@2:   bool operator!=(const linear_congruential& rhs) const
williamr@2:   { return !(*this == rhs); }
williamr@2: 
williamr@2: #else 
williamr@2:   friend bool operator==(const linear_congruential& x,
williamr@2:                          const linear_congruential& y)
williamr@2:   { return x._x == y._x; }
williamr@2:   friend bool operator!=(const linear_congruential& x,
williamr@2:                          const linear_congruential& y)
williamr@2:   { return !(x == y); }
williamr@2:     
williamr@2: #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
williamr@2:   template<class CharT, class Traits>
williamr@2:   friend std::basic_ostream<CharT,Traits>&
williamr@2:   operator<<(std::basic_ostream<CharT,Traits>& os,
williamr@2:              const linear_congruential& lcg)
williamr@2:   {
williamr@2:     return os << lcg._x;
williamr@2:   }
williamr@2: 
williamr@2:   template<class CharT, class Traits>
williamr@2:   friend std::basic_istream<CharT,Traits>&
williamr@2:   operator>>(std::basic_istream<CharT,Traits>& is,
williamr@2:              linear_congruential& lcg)
williamr@2:   {
williamr@2:     return is >> lcg._x;
williamr@2:   }
williamr@2:  
williamr@2: private:
williamr@2: #endif
williamr@2: #endif
williamr@2:     
williamr@2:   IntType _modulus;   // work-around for gcc "divide by zero" warning in ctor
williamr@2:   IntType _x;
williamr@2: };
williamr@2: 
williamr@2: // probably needs the "no native streams" caveat for STLPort
williamr@2: #if !defined(__SGI_STL_PORT) && BOOST_WORKAROUND(__GNUC__, == 2)
williamr@2: template<class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2: std::ostream&
williamr@2: operator<<(std::ostream& os,
williamr@2:            const linear_congruential<IntType,a,c,m,val>& lcg)
williamr@2: {
williamr@2:     return os << lcg._x;
williamr@2: }
williamr@2: 
williamr@2: template<class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2: std::istream&
williamr@2: operator>>(std::istream& is,
williamr@2:            linear_congruential<IntType,a,c,m,val>& lcg)
williamr@2: {
williamr@2:     return is >> lcg._x;
williamr@2: }
williamr@2: #elif defined(BOOST_NO_OPERATORS_IN_NAMESPACE) || defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
williamr@2: template<class CharT, class Traits, class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2: std::basic_ostream<CharT,Traits>&
williamr@2: operator<<(std::basic_ostream<CharT,Traits>& os,
williamr@2:            const linear_congruential<IntType,a,c,m,val>& lcg)
williamr@2: {
williamr@2:     return os << lcg._x;
williamr@2: }
williamr@2: 
williamr@2: template<class CharT, class Traits, class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2: std::basic_istream<CharT,Traits>&
williamr@2: operator>>(std::basic_istream<CharT,Traits>& is,
williamr@2:            linear_congruential<IntType,a,c,m,val>& lcg)
williamr@2: {
williamr@2:     return is >> lcg._x;
williamr@2: }
williamr@2: #endif
williamr@2: 
williamr@2: #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
williamr@2: //  A definition is required even for integral static constants
williamr@2: template<class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2: const bool linear_congruential<IntType, a, c, m, val>::has_fixed_range;
williamr@2: template<class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2: const typename linear_congruential<IntType, a, c, m, val>::result_type linear_congruential<IntType, a, c, m, val>::min_value;
williamr@2: template<class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2: const typename linear_congruential<IntType, a, c, m, val>::result_type linear_congruential<IntType, a, c, m, val>::max_value;
williamr@2: template<class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2: const IntType linear_congruential<IntType,a,c,m,val>::modulus;
williamr@2: #endif
williamr@2: 
williamr@2: } // namespace random
williamr@2: 
williamr@2: // validation values from the publications
williamr@2: typedef random::linear_congruential<int32_t, 16807, 0, 2147483647, 
williamr@2:   1043618065> minstd_rand0;
williamr@2: typedef random::linear_congruential<int32_t, 48271, 0, 2147483647,
williamr@2:   399268537> minstd_rand;
williamr@2: 
williamr@2: 
williamr@2: #if !defined(BOOST_NO_INT64_T) && !defined(BOOST_NO_INTEGRAL_INT64_T)
williamr@2: // emulate the lrand48() C library function; requires support for uint64_t
williamr@2: class rand48 
williamr@2: {
williamr@2: public:
williamr@2:   typedef int32_t result_type;
williamr@2: #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
williamr@2:   static const bool has_fixed_range = true;
williamr@2:   static const int32_t min_value = 0;
williamr@2:   static const int32_t max_value = integer_traits<int32_t>::const_max;
williamr@2: #else
williamr@2:   enum { has_fixed_range = false };
williamr@2: #endif
williamr@2:   int32_t min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
williamr@2:   int32_t max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return std::numeric_limits<int32_t>::max BOOST_PREVENT_MACRO_SUBSTITUTION (); }
williamr@2:   
williamr@2:   explicit rand48(int32_t x0 = 1) : lcf(cnv(x0)) { }
williamr@2:   explicit rand48(uint64_t x0) : lcf(x0) { }
williamr@2:   template<class It> rand48(It& first, It last) : lcf(first, last) { }
williamr@2:   // compiler-generated copy ctor and assignment operator are fine
williamr@2:   void seed(int32_t x0 = 1) { lcf.seed(cnv(x0)); }
williamr@2:   void seed(uint64_t x0) { lcf.seed(x0); }
williamr@2:   template<class It> void seed(It& first, It last) { lcf.seed(first,last); }
williamr@2: 
williamr@2:   int32_t operator()() { return static_cast<int32_t>(lcf() >> 17); }
williamr@2:   // by experiment from lrand48()
williamr@2:   static bool validation(int32_t x) { return x == 1993516219; }
williamr@2: 
williamr@2: #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
williamr@2: 
williamr@2: #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
williamr@2:   template<class CharT,class Traits>
williamr@2:   friend std::basic_ostream<CharT,Traits>&
williamr@2:   operator<<(std::basic_ostream<CharT,Traits>& os, const rand48& r)
williamr@2:   { os << r.lcf; return os; }
williamr@2: 
williamr@2:   template<class CharT,class Traits>
williamr@2:   friend std::basic_istream<CharT,Traits>&
williamr@2:   operator>>(std::basic_istream<CharT,Traits>& is, rand48& r)
williamr@2:   { is >> r.lcf; return is; }
williamr@2: #endif
williamr@2: 
williamr@2:   friend bool operator==(const rand48& x, const rand48& y)
williamr@2:   { return x.lcf == y.lcf; }
williamr@2:   friend bool operator!=(const rand48& x, const rand48& y)
williamr@2:   { return !(x == y); }
williamr@2: #else
williamr@2:   // Use a member function; Streamable concept not supported.
williamr@2:   bool operator==(const rand48& rhs) const
williamr@2:   { return lcf == rhs.lcf; }
williamr@2:   bool operator!=(const rand48& rhs) const
williamr@2:   { return !(*this == rhs); }
williamr@2: #endif
williamr@2: private:
williamr@2:   random::linear_congruential<uint64_t,
williamr@2:     uint64_t(0xDEECE66DUL) | (uint64_t(0x5) << 32), // xxxxULL is not portable
williamr@2:     0xB, uint64_t(1)<<48, /* unknown */ 0> lcf;
williamr@2:   static uint64_t cnv(int32_t x) 
williamr@2:   { return (static_cast<uint64_t>(x) << 16) | 0x330e;  }
williamr@2: };
williamr@2: #endif /* !BOOST_NO_INT64_T && !BOOST_NO_INTEGRAL_INT64_T */
williamr@2: 
williamr@2: } // namespace boost
williamr@2: 
williamr@2: #endif // BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP