williamr@2: /* boost random/inversive_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: inversive_congruential.hpp,v 1.11 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_INVERSIVE_CONGRUENTIAL_HPP
williamr@2: #define BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
williamr@2: 
williamr@2: #include <iostream>
williamr@2: #include <cassert>
williamr@2: #include <boost/config.hpp>
williamr@2: #include <boost/static_assert.hpp>
williamr@2: #include <boost/random/detail/const_mod.hpp>
williamr@2: 
williamr@2: namespace boost {
williamr@2: namespace random {
williamr@2: 
williamr@2: // Eichenauer and Lehn 1986
williamr@2: template<class IntType, IntType a, IntType b, IntType p, IntType val>
williamr@2: class inversive_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 = (b == 0 ? 1 : 0);
williamr@2:   static const result_type max_value = p-1;
williamr@2: #else
williamr@2:   BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
williamr@2: #endif
williamr@2:   BOOST_STATIC_CONSTANT(result_type, multiplier = a);
williamr@2:   BOOST_STATIC_CONSTANT(result_type, increment = b);
williamr@2:   BOOST_STATIC_CONSTANT(result_type, modulus = p);
williamr@2: 
williamr@2:   result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return b == 0 ? 1 : 0; }
williamr@2:   result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return p-1; }
williamr@2: 
williamr@2:   explicit inversive_congruential(IntType y0 = 1) : value(y0)
williamr@2:   {
williamr@2:     BOOST_STATIC_ASSERT(b >= 0);
williamr@2:     BOOST_STATIC_ASSERT(p > 1);
williamr@2:     BOOST_STATIC_ASSERT(a >= 1);
williamr@2:     if(b == 0) 
williamr@2:       assert(y0 > 0); 
williamr@2:   }
williamr@2:   template<class It> inversive_congruential(It& first, It last)
williamr@2:   { seed(first, last); }
williamr@2: 
williamr@2:   void seed(IntType y0 = 1) { value = y0; if(b == 0) assert(y0 > 0); }
williamr@2:   template<class It> void seed(It& first, It last)
williamr@2:   {
williamr@2:     if(first == last)
williamr@2:       throw std::invalid_argument("inversive_congruential::seed");
williamr@2:     value = *first++;
williamr@2:   }
williamr@2:   IntType operator()()
williamr@2:   {
williamr@2:     typedef const_mod<IntType, p> do_mod;
williamr@2:     value = do_mod::mult_add(a, do_mod::invert(value), b);
williamr@2:     return value;
williamr@2:   }
williamr@2: 
williamr@2:   bool validation(result_type x) const { return val == x; }
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, inversive_congruential x)
williamr@2:   { os << x.value; 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, inversive_congruential& x)
williamr@2:   { is >> x.value; return is; }
williamr@2: #endif
williamr@2: 
williamr@2:   friend bool operator==(inversive_congruential x, inversive_congruential y)
williamr@2:   { return x.value == y.value; }
williamr@2:   friend bool operator!=(inversive_congruential x, inversive_congruential y)
williamr@2:   { return !(x == y); }
williamr@2: #else
williamr@2:   // Use a member function; Streamable concept not supported.
williamr@2:   bool operator==(inversive_congruential rhs) const
williamr@2:   { return value == rhs.value; }
williamr@2:   bool operator!=(inversive_congruential rhs) const
williamr@2:   { return !(*this == rhs); }
williamr@2: #endif
williamr@2: private:
williamr@2:   IntType value;
williamr@2: };
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 b, IntType p, IntType val>
williamr@2: const bool inversive_congruential<IntType, a, b, p, val>::has_fixed_range;
williamr@2: template<class IntType, IntType a, IntType b, IntType p, IntType val>
williamr@2: const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::min_value;
williamr@2: template<class IntType, IntType a, IntType b, IntType p, IntType val>
williamr@2: const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::max_value;
williamr@2: template<class IntType, IntType a, IntType b, IntType p, IntType val>
williamr@2: const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::multiplier;
williamr@2: template<class IntType, IntType a, IntType b, IntType p, IntType val>
williamr@2: const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::increment;
williamr@2: template<class IntType, IntType a, IntType b, IntType p, IntType val>
williamr@2: const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::modulus;
williamr@2: #endif
williamr@2: 
williamr@2: } // namespace random
williamr@2: 
williamr@2: typedef random::inversive_congruential<int32_t, 9102, 2147483647-36884165,
williamr@2:   2147483647, 0> hellekalek1995;
williamr@2: 
williamr@2: } // namespace boost
williamr@2: 
williamr@2: #endif // BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP