williamr@2
|
1 |
/* boost random/inversive_congruential.hpp header file
|
williamr@2
|
2 |
*
|
williamr@2
|
3 |
* Copyright Jens Maurer 2000-2001
|
williamr@2
|
4 |
* Distributed under the Boost Software License, Version 1.0. (See
|
williamr@2
|
5 |
* accompanying file LICENSE_1_0.txt or copy at
|
williamr@2
|
6 |
* http://www.boost.org/LICENSE_1_0.txt)
|
williamr@2
|
7 |
*
|
williamr@2
|
8 |
* See http://www.boost.org for most recent version including documentation.
|
williamr@2
|
9 |
*
|
williamr@2
|
10 |
* $Id: inversive_congruential.hpp,v 1.11 2005/05/21 15:57:00 dgregor Exp $
|
williamr@2
|
11 |
*
|
williamr@2
|
12 |
* Revision history
|
williamr@2
|
13 |
* 2001-02-18 moved to individual header files
|
williamr@2
|
14 |
*/
|
williamr@2
|
15 |
|
williamr@2
|
16 |
#ifndef BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
|
williamr@2
|
17 |
#define BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
|
williamr@2
|
18 |
|
williamr@2
|
19 |
#include <iostream>
|
williamr@2
|
20 |
#include <cassert>
|
williamr@2
|
21 |
#include <boost/config.hpp>
|
williamr@2
|
22 |
#include <boost/static_assert.hpp>
|
williamr@2
|
23 |
#include <boost/random/detail/const_mod.hpp>
|
williamr@2
|
24 |
|
williamr@2
|
25 |
namespace boost {
|
williamr@2
|
26 |
namespace random {
|
williamr@2
|
27 |
|
williamr@2
|
28 |
// Eichenauer and Lehn 1986
|
williamr@2
|
29 |
template<class IntType, IntType a, IntType b, IntType p, IntType val>
|
williamr@2
|
30 |
class inversive_congruential
|
williamr@2
|
31 |
{
|
williamr@2
|
32 |
public:
|
williamr@2
|
33 |
typedef IntType result_type;
|
williamr@2
|
34 |
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
|
williamr@2
|
35 |
static const bool has_fixed_range = true;
|
williamr@2
|
36 |
static const result_type min_value = (b == 0 ? 1 : 0);
|
williamr@2
|
37 |
static const result_type max_value = p-1;
|
williamr@2
|
38 |
#else
|
williamr@2
|
39 |
BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
|
williamr@2
|
40 |
#endif
|
williamr@2
|
41 |
BOOST_STATIC_CONSTANT(result_type, multiplier = a);
|
williamr@2
|
42 |
BOOST_STATIC_CONSTANT(result_type, increment = b);
|
williamr@2
|
43 |
BOOST_STATIC_CONSTANT(result_type, modulus = p);
|
williamr@2
|
44 |
|
williamr@2
|
45 |
result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return b == 0 ? 1 : 0; }
|
williamr@2
|
46 |
result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return p-1; }
|
williamr@2
|
47 |
|
williamr@2
|
48 |
explicit inversive_congruential(IntType y0 = 1) : value(y0)
|
williamr@2
|
49 |
{
|
williamr@2
|
50 |
BOOST_STATIC_ASSERT(b >= 0);
|
williamr@2
|
51 |
BOOST_STATIC_ASSERT(p > 1);
|
williamr@2
|
52 |
BOOST_STATIC_ASSERT(a >= 1);
|
williamr@2
|
53 |
if(b == 0)
|
williamr@2
|
54 |
assert(y0 > 0);
|
williamr@2
|
55 |
}
|
williamr@2
|
56 |
template<class It> inversive_congruential(It& first, It last)
|
williamr@2
|
57 |
{ seed(first, last); }
|
williamr@2
|
58 |
|
williamr@2
|
59 |
void seed(IntType y0 = 1) { value = y0; if(b == 0) assert(y0 > 0); }
|
williamr@2
|
60 |
template<class It> void seed(It& first, It last)
|
williamr@2
|
61 |
{
|
williamr@2
|
62 |
if(first == last)
|
williamr@2
|
63 |
throw std::invalid_argument("inversive_congruential::seed");
|
williamr@2
|
64 |
value = *first++;
|
williamr@2
|
65 |
}
|
williamr@2
|
66 |
IntType operator()()
|
williamr@2
|
67 |
{
|
williamr@2
|
68 |
typedef const_mod<IntType, p> do_mod;
|
williamr@2
|
69 |
value = do_mod::mult_add(a, do_mod::invert(value), b);
|
williamr@2
|
70 |
return value;
|
williamr@2
|
71 |
}
|
williamr@2
|
72 |
|
williamr@2
|
73 |
bool validation(result_type x) const { return val == x; }
|
williamr@2
|
74 |
|
williamr@2
|
75 |
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
|
williamr@2
|
76 |
|
williamr@2
|
77 |
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
|
williamr@2
|
78 |
template<class CharT, class Traits>
|
williamr@2
|
79 |
friend std::basic_ostream<CharT,Traits>&
|
williamr@2
|
80 |
operator<<(std::basic_ostream<CharT,Traits>& os, inversive_congruential x)
|
williamr@2
|
81 |
{ os << x.value; return os; }
|
williamr@2
|
82 |
|
williamr@2
|
83 |
template<class CharT, class Traits>
|
williamr@2
|
84 |
friend std::basic_istream<CharT,Traits>&
|
williamr@2
|
85 |
operator>>(std::basic_istream<CharT,Traits>& is, inversive_congruential& x)
|
williamr@2
|
86 |
{ is >> x.value; return is; }
|
williamr@2
|
87 |
#endif
|
williamr@2
|
88 |
|
williamr@2
|
89 |
friend bool operator==(inversive_congruential x, inversive_congruential y)
|
williamr@2
|
90 |
{ return x.value == y.value; }
|
williamr@2
|
91 |
friend bool operator!=(inversive_congruential x, inversive_congruential y)
|
williamr@2
|
92 |
{ return !(x == y); }
|
williamr@2
|
93 |
#else
|
williamr@2
|
94 |
// Use a member function; Streamable concept not supported.
|
williamr@2
|
95 |
bool operator==(inversive_congruential rhs) const
|
williamr@2
|
96 |
{ return value == rhs.value; }
|
williamr@2
|
97 |
bool operator!=(inversive_congruential rhs) const
|
williamr@2
|
98 |
{ return !(*this == rhs); }
|
williamr@2
|
99 |
#endif
|
williamr@2
|
100 |
private:
|
williamr@2
|
101 |
IntType value;
|
williamr@2
|
102 |
};
|
williamr@2
|
103 |
|
williamr@2
|
104 |
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
|
williamr@2
|
105 |
// A definition is required even for integral static constants
|
williamr@2
|
106 |
template<class IntType, IntType a, IntType b, IntType p, IntType val>
|
williamr@2
|
107 |
const bool inversive_congruential<IntType, a, b, p, val>::has_fixed_range;
|
williamr@2
|
108 |
template<class IntType, IntType a, IntType b, IntType p, IntType val>
|
williamr@2
|
109 |
const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::min_value;
|
williamr@2
|
110 |
template<class IntType, IntType a, IntType b, IntType p, IntType val>
|
williamr@2
|
111 |
const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::max_value;
|
williamr@2
|
112 |
template<class IntType, IntType a, IntType b, IntType p, IntType val>
|
williamr@2
|
113 |
const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::multiplier;
|
williamr@2
|
114 |
template<class IntType, IntType a, IntType b, IntType p, IntType val>
|
williamr@2
|
115 |
const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::increment;
|
williamr@2
|
116 |
template<class IntType, IntType a, IntType b, IntType p, IntType val>
|
williamr@2
|
117 |
const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::modulus;
|
williamr@2
|
118 |
#endif
|
williamr@2
|
119 |
|
williamr@2
|
120 |
} // namespace random
|
williamr@2
|
121 |
|
williamr@2
|
122 |
typedef random::inversive_congruential<int32_t, 9102, 2147483647-36884165,
|
williamr@2
|
123 |
2147483647, 0> hellekalek1995;
|
williamr@2
|
124 |
|
williamr@2
|
125 |
} // namespace boost
|
williamr@2
|
126 |
|
williamr@2
|
127 |
#endif // BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
|