1 /* boost random/inversive_congruential.hpp header file
\r
3 * Copyright Jens Maurer 2000-2001
\r
4 * Distributed under the Boost Software License, Version 1.0. (See
\r
5 * accompanying file LICENSE_1_0.txt or copy at
\r
6 * http://www.boost.org/LICENSE_1_0.txt)
\r
8 * See http://www.boost.org for most recent version including documentation.
\r
10 * $Id: inversive_congruential.hpp 52492 2009-04-19 14:55:57Z steven_watanabe $
\r
13 * 2001-02-18 moved to individual header files
\r
16 #ifndef BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
\r
17 #define BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
\r
21 #include <stdexcept>
\r
22 #include <boost/config.hpp>
\r
23 #include <boost/static_assert.hpp>
\r
24 #include <boost/random/detail/config.hpp>
\r
25 #include <boost/random/detail/const_mod.hpp>
\r
30 // Eichenauer and Lehn 1986
\r
31 template<class IntType, IntType a, IntType b, IntType p, IntType val>
\r
32 class inversive_congruential
\r
35 typedef IntType result_type;
\r
36 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
\r
37 static const bool has_fixed_range = true;
\r
38 static const result_type min_value = (b == 0 ? 1 : 0);
\r
39 static const result_type max_value = p-1;
\r
41 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
\r
43 BOOST_STATIC_CONSTANT(result_type, multiplier = a);
\r
44 BOOST_STATIC_CONSTANT(result_type, increment = b);
\r
45 BOOST_STATIC_CONSTANT(result_type, modulus = p);
\r
47 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return b == 0 ? 1 : 0; }
\r
48 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return p-1; }
\r
50 explicit inversive_congruential(IntType y0 = 1) : value(y0)
\r
52 BOOST_STATIC_ASSERT(b >= 0);
\r
53 BOOST_STATIC_ASSERT(p > 1);
\r
54 BOOST_STATIC_ASSERT(a >= 1);
\r
58 template<class It> inversive_congruential(It& first, It last)
\r
59 { seed(first, last); }
\r
61 void seed(IntType y0 = 1) { value = y0; if(b == 0) assert(y0 > 0); }
\r
62 template<class It> void seed(It& first, It last)
\r
65 throw std::invalid_argument("inversive_congruential::seed");
\r
68 IntType operator()()
\r
70 typedef const_mod<IntType, p> do_mod;
\r
71 value = do_mod::mult_add(a, do_mod::invert(value), b);
\r
75 static bool validation(result_type x) { return val == x; }
\r
77 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
\r
79 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
\r
80 template<class CharT, class Traits>
\r
81 friend std::basic_ostream<CharT,Traits>&
\r
82 operator<<(std::basic_ostream<CharT,Traits>& os, inversive_congruential x)
\r
83 { os << x.value; return os; }
\r
85 template<class CharT, class Traits>
\r
86 friend std::basic_istream<CharT,Traits>&
\r
87 operator>>(std::basic_istream<CharT,Traits>& is, inversive_congruential& x)
\r
88 { is >> x.value; return is; }
\r
91 friend bool operator==(inversive_congruential x, inversive_congruential y)
\r
92 { return x.value == y.value; }
\r
93 friend bool operator!=(inversive_congruential x, inversive_congruential y)
\r
94 { return !(x == y); }
\r
96 // Use a member function; Streamable concept not supported.
\r
97 bool operator==(inversive_congruential rhs) const
\r
98 { return value == rhs.value; }
\r
99 bool operator!=(inversive_congruential rhs) const
\r
100 { return !(*this == rhs); }
\r
106 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
\r
107 // A definition is required even for integral static constants
\r
108 template<class IntType, IntType a, IntType b, IntType p, IntType val>
\r
109 const bool inversive_congruential<IntType, a, b, p, val>::has_fixed_range;
\r
110 template<class IntType, IntType a, IntType b, IntType p, IntType val>
\r
111 const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::min_value;
\r
112 template<class IntType, IntType a, IntType b, IntType p, IntType val>
\r
113 const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::max_value;
\r
114 template<class IntType, IntType a, IntType b, IntType p, IntType val>
\r
115 const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::multiplier;
\r
116 template<class IntType, IntType a, IntType b, IntType p, IntType val>
\r
117 const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::increment;
\r
118 template<class IntType, IntType a, IntType b, IntType p, IntType val>
\r
119 const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::modulus;
\r
122 } // namespace random
\r
124 typedef random::inversive_congruential<int32_t, 9102, 2147483647-36884165,
\r
125 2147483647, 0> hellekalek1995;
\r
127 } // namespace boost
\r
129 #endif // BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
\r