[29] | 1 | /* boost random/inversive_congruential.hpp header file |
---|
| 2 | * |
---|
| 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) |
---|
| 7 | * |
---|
| 8 | * See http://www.boost.org for most recent version including documentation. |
---|
| 9 | * |
---|
| 10 | * $Id: inversive_congruential.hpp,v 1.11 2005/05/21 15:57:00 dgregor Exp $ |
---|
| 11 | * |
---|
| 12 | * Revision history |
---|
| 13 | * 2001-02-18 moved to individual header files |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | #ifndef BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP |
---|
| 17 | #define BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP |
---|
| 18 | |
---|
| 19 | #include <iostream> |
---|
| 20 | #include <cassert> |
---|
| 21 | #include <boost/config.hpp> |
---|
| 22 | #include <boost/static_assert.hpp> |
---|
| 23 | #include <boost/random/detail/const_mod.hpp> |
---|
| 24 | |
---|
| 25 | namespace boost { |
---|
| 26 | namespace random { |
---|
| 27 | |
---|
| 28 | // Eichenauer and Lehn 1986 |
---|
| 29 | template<class IntType, IntType a, IntType b, IntType p, IntType val> |
---|
| 30 | class inversive_congruential |
---|
| 31 | { |
---|
| 32 | public: |
---|
| 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; |
---|
| 38 | #else |
---|
| 39 | BOOST_STATIC_CONSTANT(bool, has_fixed_range = false); |
---|
| 40 | #endif |
---|
| 41 | BOOST_STATIC_CONSTANT(result_type, multiplier = a); |
---|
| 42 | BOOST_STATIC_CONSTANT(result_type, increment = b); |
---|
| 43 | BOOST_STATIC_CONSTANT(result_type, modulus = p); |
---|
| 44 | |
---|
| 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; } |
---|
| 47 | |
---|
| 48 | explicit inversive_congruential(IntType y0 = 1) : value(y0) |
---|
| 49 | { |
---|
| 50 | BOOST_STATIC_ASSERT(b >= 0); |
---|
| 51 | BOOST_STATIC_ASSERT(p > 1); |
---|
| 52 | BOOST_STATIC_ASSERT(a >= 1); |
---|
| 53 | if(b == 0) |
---|
| 54 | assert(y0 > 0); |
---|
| 55 | } |
---|
| 56 | template<class It> inversive_congruential(It& first, It last) |
---|
| 57 | { seed(first, last); } |
---|
| 58 | |
---|
| 59 | void seed(IntType y0 = 1) { value = y0; if(b == 0) assert(y0 > 0); } |
---|
| 60 | template<class It> void seed(It& first, It last) |
---|
| 61 | { |
---|
| 62 | if(first == last) |
---|
| 63 | throw std::invalid_argument("inversive_congruential::seed"); |
---|
| 64 | value = *first++; |
---|
| 65 | } |
---|
| 66 | IntType operator()() |
---|
| 67 | { |
---|
| 68 | typedef const_mod<IntType, p> do_mod; |
---|
| 69 | value = do_mod::mult_add(a, do_mod::invert(value), b); |
---|
| 70 | return value; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | bool validation(result_type x) const { return val == x; } |
---|
| 74 | |
---|
| 75 | #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE |
---|
| 76 | |
---|
| 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; } |
---|
| 82 | |
---|
| 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; } |
---|
| 87 | #endif |
---|
| 88 | |
---|
| 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) |
---|
| 92 | { return !(x == y); } |
---|
| 93 | #else |
---|
| 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); } |
---|
| 99 | #endif |
---|
| 100 | private: |
---|
| 101 | IntType value; |
---|
| 102 | }; |
---|
| 103 | |
---|
| 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; |
---|
| 118 | #endif |
---|
| 119 | |
---|
| 120 | } // namespace random |
---|
| 121 | |
---|
| 122 | typedef random::inversive_congruential<int32_t, 9102, 2147483647-36884165, |
---|
| 123 | 2147483647, 0> hellekalek1995; |
---|
| 124 | |
---|
| 125 | } // namespace boost |
---|
| 126 | |
---|
| 127 | #endif // BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP |
---|