source: NonGTP/Boost/boost/random/inversive_congruential.hpp @ 857

Revision 857, 4.7 KB checked in by igarcia, 19 years ago (diff)
RevLine 
[857]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
25namespace boost {
26namespace random {
27
28// Eichenauer and Lehn 1986
29template<class IntType, IntType a, IntType b, IntType p, IntType val>
30class inversive_congruential
31{
32public:
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
100private:
101  IntType value;
102};
103
104#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
105//  A definition is required even for integral static constants
106template<class IntType, IntType a, IntType b, IntType p, IntType val>
107const bool inversive_congruential<IntType, a, b, p, val>::has_fixed_range;
108template<class IntType, IntType a, IntType b, IntType p, IntType val>
109const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::min_value;
110template<class IntType, IntType a, IntType b, IntType p, IntType val>
111const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::max_value;
112template<class IntType, IntType a, IntType b, IntType p, IntType val>
113const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::multiplier;
114template<class IntType, IntType a, IntType b, IntType p, IntType val>
115const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::increment;
116template<class IntType, IntType a, IntType b, IntType p, IntType val>
117const 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
122typedef random::inversive_congruential<int32_t, 9102, 2147483647-36884165,
123  2147483647, 0> hellekalek1995;
124
125} // namespace boost
126
127#endif // BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
Note: See TracBrowser for help on using the repository browser.