1 /* boost random/tausworthe.hpp header file
3 * Copyright Jens Maurer 2002
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)
8 * See http://www.boost.org for most recent version including documentation.
10 * $Id: linear_feedback_shift.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
14 #ifndef BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
15 #define BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
20 #include <boost/config.hpp>
21 #include <boost/static_assert.hpp>
22 #include <boost/limits.hpp>
23 #include <boost/random/detail/config.hpp>
29 * Instatiation of @c linear_feedback_shift model a
30 * \pseudo_random_number_generator. It was originally
34 * "Random numbers generated by linear recurrence modulo two.",
35 * Tausworthe, R. C.(1965), Mathematics of Computation 19, 201-209.
38 template<class UIntType, int w, int k, int q, int s, UIntType val>
39 class linear_feedback_shift
42 typedef UIntType result_type;
43 // avoid the warning trouble when using (1<<w) on 32 bit machines
44 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
45 BOOST_STATIC_CONSTANT(int, word_size = w);
46 BOOST_STATIC_CONSTANT(int, exponent1 = k);
47 BOOST_STATIC_CONSTANT(int, exponent2 = q);
48 BOOST_STATIC_CONSTANT(int, step_size = s);
50 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
51 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return wordmask; }
53 // MSVC 6 and possibly others crash when encountering complicated integral
54 // constant expressions. Avoid the checks for now.
55 // BOOST_STATIC_ASSERT(w > 0);
56 // BOOST_STATIC_ASSERT(q > 0);
57 // BOOST_STATIC_ASSERT(k < w);
58 // BOOST_STATIC_ASSERT(0 < 2*q && 2*q < k);
59 // BOOST_STATIC_ASSERT(0 < s && s <= k-q);
61 explicit linear_feedback_shift(UIntType s0 = 341) : wordmask(0)
63 // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
64 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
65 BOOST_STATIC_ASSERT(std::numeric_limits<UIntType>::is_integer);
66 BOOST_STATIC_ASSERT(!std::numeric_limits<UIntType>::is_signed);
69 // avoid "left shift count >= with of type" warning
70 for(int i = 0; i < w; ++i)
71 wordmask |= (1u << i);
75 template<class It> linear_feedback_shift(It& first, It last) : wordmask(0)
77 // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
78 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
79 BOOST_STATIC_ASSERT(std::numeric_limits<UIntType>::is_integer);
80 BOOST_STATIC_ASSERT(!std::numeric_limits<UIntType>::is_signed);
83 // avoid "left shift count >= with of type" warning
84 for(int i = 0; i < w; ++i)
85 wordmask |= (1u << i);
89 void seed(UIntType s0 = 341) {
90 if(s0 < (1 << (w-k))) {
95 template<class It> void seed(It& first, It last)
98 throw std::invalid_argument("linear_feedback_shift::seed");
100 assert(value >= (1 << (w-k)));
103 result_type operator()()
105 const UIntType b = (((value << q) ^ value) & wordmask) >> (k-s);
106 const UIntType mask = ( (~static_cast<UIntType>(0)) << (w-k) ) & wordmask;
107 value = ((value & mask) << s) ^ b;
110 static bool validation(result_type x) { return val == x; }
112 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
114 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
115 template<class CharT, class Traits>
116 friend std::basic_ostream<CharT,Traits>&
117 operator<<(std::basic_ostream<CharT,Traits>& os, linear_feedback_shift x)
118 { os << x.value; return os; }
120 template<class CharT, class Traits>
121 friend std::basic_istream<CharT,Traits>&
122 operator>>(std::basic_istream<CharT,Traits>& is, linear_feedback_shift& x)
123 { is >> x.value; return is; }
126 friend bool operator==(linear_feedback_shift x, linear_feedback_shift y)
127 { return x.value == y.value; }
128 friend bool operator!=(linear_feedback_shift x, linear_feedback_shift y)
129 { return !(x == y); }
131 // Use a member function; Streamable concept not supported.
132 bool operator==(linear_feedback_shift rhs) const
133 { return value == rhs.value; }
134 bool operator!=(linear_feedback_shift rhs) const
135 { return !(*this == rhs); }
139 UIntType wordmask; // avoid "left shift count >= width of type" warnings
143 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
144 // A definition is required even for integral static constants
145 template<class UIntType, int w, int k, int q, int s, UIntType val>
146 const bool linear_feedback_shift<UIntType, w, k, q, s, val>::has_fixed_range;
147 template<class UIntType, int w, int k, int q, int s, UIntType val>
148 const int linear_feedback_shift<UIntType, w, k, q, s, val>::word_size;
149 template<class UIntType, int w, int k, int q, int s, UIntType val>
150 const int linear_feedback_shift<UIntType, w, k, q, s, val>::exponent1;
151 template<class UIntType, int w, int k, int q, int s, UIntType val>
152 const int linear_feedback_shift<UIntType, w, k, q, s, val>::exponent2;
153 template<class UIntType, int w, int k, int q, int s, UIntType val>
154 const int linear_feedback_shift<UIntType, w, k, q, s, val>::step_size;
157 } // namespace random
160 #endif // BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP