1 /* boost random/inversive_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: inversive_congruential.hpp,v 1.11 2005/05/21 15:57:00 dgregor Exp $
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
17 #define BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
21 #include <boost/config.hpp>
22 #include <boost/static_assert.hpp>
23 #include <boost/random/detail/const_mod.hpp>
28 // Eichenauer and Lehn 1986
29 template<class IntType, IntType a, IntType b, IntType p, IntType val>
30 class inversive_congruential
33 typedef IntType result_type;
34 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
35 static const bool has_fixed_range = true;
36 static const result_type min_value = (b == 0 ? 1 : 0);
37 static const result_type max_value = p-1;
39 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
41 BOOST_STATIC_CONSTANT(result_type, multiplier = a);
42 BOOST_STATIC_CONSTANT(result_type, increment = b);
43 BOOST_STATIC_CONSTANT(result_type, modulus = p);
45 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return b == 0 ? 1 : 0; }
46 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return p-1; }
48 explicit inversive_congruential(IntType y0 = 1) : value(y0)
50 BOOST_STATIC_ASSERT(b >= 0);
51 BOOST_STATIC_ASSERT(p > 1);
52 BOOST_STATIC_ASSERT(a >= 1);
56 template<class It> inversive_congruential(It& first, It last)
57 { seed(first, last); }
59 void seed(IntType y0 = 1) { value = y0; if(b == 0) assert(y0 > 0); }
60 template<class It> void seed(It& first, It last)
63 throw std::invalid_argument("inversive_congruential::seed");
68 typedef const_mod<IntType, p> do_mod;
69 value = do_mod::mult_add(a, do_mod::invert(value), b);
73 bool validation(result_type x) const { return val == x; }
75 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
77 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
78 template<class CharT, class Traits>
79 friend std::basic_ostream<CharT,Traits>&
80 operator<<(std::basic_ostream<CharT,Traits>& os, inversive_congruential x)
81 { os << x.value; return os; }
83 template<class CharT, class Traits>
84 friend std::basic_istream<CharT,Traits>&
85 operator>>(std::basic_istream<CharT,Traits>& is, inversive_congruential& x)
86 { is >> x.value; return is; }
89 friend bool operator==(inversive_congruential x, inversive_congruential y)
90 { return x.value == y.value; }
91 friend bool operator!=(inversive_congruential x, inversive_congruential y)
94 // Use a member function; Streamable concept not supported.
95 bool operator==(inversive_congruential rhs) const
96 { return value == rhs.value; }
97 bool operator!=(inversive_congruential rhs) const
98 { return !(*this == rhs); }
104 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
105 // A definition is required even for integral static constants
106 template<class IntType, IntType a, IntType b, IntType p, IntType val>
107 const bool inversive_congruential<IntType, a, b, p, val>::has_fixed_range;
108 template<class IntType, IntType a, IntType b, IntType p, IntType val>
109 const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::min_value;
110 template<class IntType, IntType a, IntType b, IntType p, IntType val>
111 const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::max_value;
112 template<class IntType, IntType a, IntType b, IntType p, IntType val>
113 const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::multiplier;
114 template<class IntType, IntType a, IntType b, IntType p, IntType val>
115 const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::increment;
116 template<class IntType, IntType a, IntType b, IntType p, IntType val>
117 const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::modulus;
120 } // namespace random
122 typedef random::inversive_congruential<int32_t, 9102, 2147483647-36884165,
123 2147483647, 0> hellekalek1995;
127 #endif // BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP