]> AND Private Git Repository - canny.git/blob - stc/exp/ml_stc_linux_make_v1.0/include/boost/random/inversive_congruential.hpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
294cd5232086c7f4f5b13dd9087fc93e7127be1a
[canny.git] / stc / exp / ml_stc_linux_make_v1.0 / include / boost / random / inversive_congruential.hpp
1 /* boost random/inversive_congruential.hpp header file\r
2  *\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
7  *\r
8  * See http://www.boost.org for most recent version including documentation.\r
9  *\r
10  * $Id: inversive_congruential.hpp 52492 2009-04-19 14:55:57Z steven_watanabe $\r
11  *\r
12  * Revision history\r
13  *  2001-02-18  moved to individual header files\r
14  */\r
15 \r
16 #ifndef BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP\r
17 #define BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP\r
18 \r
19 #include <iostream>\r
20 #include <cassert>\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
26 \r
27 namespace boost {\r
28 namespace random {\r
29 \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
33 {\r
34 public:\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
40 #else\r
41   BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);\r
42 #endif\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
46 \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
49 \r
50   explicit inversive_congruential(IntType y0 = 1) : value(y0)\r
51   {\r
52     BOOST_STATIC_ASSERT(b >= 0);\r
53     BOOST_STATIC_ASSERT(p > 1);\r
54     BOOST_STATIC_ASSERT(a >= 1);\r
55     if(b == 0) \r
56       assert(y0 > 0); \r
57   }\r
58   template<class It> inversive_congruential(It& first, It last)\r
59   { seed(first, last); }\r
60 \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
63   {\r
64     if(first == last)\r
65       throw std::invalid_argument("inversive_congruential::seed");\r
66     value = *first++;\r
67   }\r
68   IntType operator()()\r
69   {\r
70     typedef const_mod<IntType, p> do_mod;\r
71     value = do_mod::mult_add(a, do_mod::invert(value), b);\r
72     return value;\r
73   }\r
74 \r
75   static bool validation(result_type x) { return val == x; }\r
76 \r
77 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE\r
78 \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
84 \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
89 #endif\r
90 \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
95 #else\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
101 #endif\r
102 private:\r
103   IntType value;\r
104 };\r
105 \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
120 #endif\r
121 \r
122 } // namespace random\r
123 \r
124 typedef random::inversive_congruential<int32_t, 9102, 2147483647-36884165,\r
125   2147483647, 0> hellekalek1995;\r
126 \r
127 } // namespace boost\r
128 \r
129 #endif // BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP\r