1 // random number generation (out of line) -*- C++ -*-
3 // Copyright (C) 2006 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
34 _GLIBCXX_BEGIN_NAMESPACE(tr1)
37 * Implementation-space details.
41 // General case for x = (ax + c) mod m -- use Schrage's algorithm to avoid
44 // Because a and c are compile-time integral constants the compiler kindly
45 // elides any unreachable paths.
47 // Preconditions: a > 0, m > 0.
49 template<typename _Tp, _Tp a, _Tp c, _Tp m, bool _m_is_zero>
59 static const _Tp q = m / a;
60 static const _Tp r = m % a;
82 // Special case for m==0 -- use unsigned integer overflow as modulo
84 template<typename _Tp, _Tp a, _Tp c, _Tp m>
85 struct Mod<_Tp, a, c, m, true>
92 // Dispatch based on modulus value to prevent divide-by-zero compile-time
93 // errors when m == 0.
94 template<typename _Tp, _Tp a, _Tp c, _Tp m>
97 { return Mod<_Tp, a, c, m, m == 0>::calc(x); }
99 // Like the above, for a==1, c==0, in terms of w.
100 template<typename _Tp, _Tp w, bool>
105 { return x % (_Tp(1) << w); }
108 template<typename _Tp, _Tp w>
109 struct Mod_w<_Tp, w, true>
116 template<typename _Tp, _Tp w>
119 { return Mod_w<_Tp, w, w == std::numeric_limits<_Tp>::digits>::calc(x); }
121 // Selector to return the maximum value possible that will fit in
122 // @p w bits of @p _Tp.
123 template<typename _Tp, _Tp w, bool>
128 { return (_Tp(1) << w) - 1; }
131 template<typename _Tp, _Tp w>
132 struct Max_w<_Tp, w, true>
136 { return std::numeric_limits<_Tp>::max(); }
139 } // namespace _Private
143 * Constructs the LCR engine with integral seed @p x0.
145 template<class UIntType, UIntType a, UIntType c, UIntType m>
146 linear_congruential<UIntType, a, c, m>::
147 linear_congruential(unsigned long x0)
151 * Constructs the LCR engine with seed generated from @p g.
153 template<class UIntType, UIntType a, UIntType c, UIntType m>
155 linear_congruential<UIntType, a, c, m>::
156 linear_congruential(Gen& g)
160 * Seeds the LCR with integral value @p x0, adjusted so that the
161 * ring identity is never a member of the convergence set.
163 template<class UIntType, UIntType a, UIntType c, UIntType m>
165 linear_congruential<UIntType, a, c, m>::
166 seed(unsigned long x0)
168 if ((_Private::mod<UIntType, 1, 0, m>(c) == 0)
169 && (_Private::mod<UIntType, 1, 0, m>(x0) == 0))
170 m_x = _Private::mod<UIntType, 1, 0, m>(1);
172 m_x = _Private::mod<UIntType, 1, 0, m>(x0);
176 * Seeds the LCR engine with a value generated by @p g.
178 template<class UIntType, UIntType a, UIntType c, UIntType m>
181 linear_congruential<UIntType, a, c, m>::
182 seed(Gen& g, false_type)
185 if ((_Private::mod<UIntType, 1, 0, m>(c) == 0)
186 && (_Private::mod<UIntType, 1, 0, m>(x0) == 0))
187 m_x = _Private::mod<UIntType, 1, 0, m>(1);
189 m_x = _Private::mod<UIntType, 1, 0, m>(x0);
193 * Returns a value that is less than or equal to all values potentially
194 * returned by operator(). The return value of this function does not
195 * change during the lifetime of the object..
197 * The minumum depends on the @p c parameter: if it is zero, the
198 * minimum generated must be > 0, otherwise 0 is allowed.
200 template<class UIntType, UIntType a, UIntType c, UIntType m>
201 typename linear_congruential<UIntType, a, c, m>::result_type
202 linear_congruential<UIntType, a, c, m>::
204 { return (_Private::mod<UIntType, 1, 0, m>(c) == 0) ? 1 : 0; }
207 * Gets the maximum possible value of the generated range.
209 * For a linear congruential generator, the maximum is always @p m - 1.
211 template<class UIntType, UIntType a, UIntType c, UIntType m>
212 typename linear_congruential<UIntType, a, c, m>::result_type
213 linear_congruential<UIntType, a, c, m>::
215 { return (m == 0) ? std::numeric_limits<UIntType>::max() : (m - 1); }
218 * Gets the next generated value in sequence.
220 template<class UIntType, UIntType a, UIntType c, UIntType m>
221 typename linear_congruential<UIntType, a, c, m>::result_type
222 linear_congruential<UIntType, a, c, m>::
225 m_x = _Private::mod<UIntType, a, c, m>(m_x);
230 template<class _UInt, int w, int n, int m, int r,
231 _UInt a, int u, int s,
232 _UInt b, int t, _UInt c, int l>
234 mersenne_twister<_UInt, w, n, m, r, a, u, s, b, t, c, l>::
235 seed(unsigned long value)
237 _M_x[0] = _Private::mod_w<_UInt, w>(value);
239 for (int i = 1; i < n; ++i)
241 _UInt x = _M_x[i - 1];
245 _M_x[i] = _Private::mod_w<_UInt, w>(x);
250 template<class _UInt, int w, int n, int m, int r,
251 _UInt a, int u, int s,
252 _UInt b, int t, _UInt c, int l>
255 mersenne_twister<_UInt, w, n, m, r, a, u, s, b, t, c, l>::
256 seed(Gen& gen, false_type)
258 for (int i = 0; i < n; ++i)
259 _M_x[i] = _Private::mod_w<_UInt, w>(gen());
263 template<class _UInt, int w, int n, int m, int r,
264 _UInt a, int u, int s,
265 _UInt b, int t, _UInt c, int l>
267 mersenne_twister<_UInt, w, n, m, r, a, u, s, b, t, c, l>::result_type
268 mersenne_twister<_UInt, w, n, m, r, a, u, s, b, t, c, l>::
271 using _Private::Max_w;
272 using std::numeric_limits;
273 return Max_w<_UInt, w, w == numeric_limits<_UInt>::digits>::value();
276 template<class _UInt, int w, int n, int m, int r,
277 _UInt a, int u, int s,
278 _UInt b, int t, _UInt c, int l>
280 mersenne_twister<_UInt, w, n, m, r, a, u, s, b, t, c, l>::result_type
281 mersenne_twister<_UInt, w, n, m, r, a, u, s, b, t, c, l>::
284 // Reload the vector - cost is O(n) amortized over n calls.
287 const _UInt upper_mask = (~_UInt()) << r;
288 const _UInt lower_mask = ~upper_mask;
290 for (int k = 0; k < (n - m); ++k)
292 _UInt y = (_M_x[k] & upper_mask) | (_M_x[k + 1] & lower_mask);
293 _M_x[k] = _M_x[k + m] ^ (y >> 1) ^ ((y & 0x01) ? a : 0);
296 for (int k = (n - m); k < (n - 1); ++k)
298 _UInt y = (_M_x[k] & upper_mask) | (_M_x[k + 1] & lower_mask);
299 _M_x[k] = _M_x[k + (m - n)] ^ (y >> 1) ^ ((y & 0x01) ? a : 0);
305 // Calculate o(x(i)).
306 result_type z = _M_x[_M_p++];
316 template<typename _IntType, _IntType m, int s, int r>
318 subtract_with_carry<_IntType, m, s, r>::
319 seed(_IntType __value)
321 std::tr1::linear_congruential<unsigned long, 40014, 0, 2147483563>
324 for (int i = 0; i < long_lag; ++i)
325 _M_x[i] = _Private::mod<_IntType, 1, 0, modulus>(lcg());
327 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
332 // This implementation differs from the tr1 spec because the tr1 spec refused
333 // to make any sense to me: the exponent of the factor in the spec goes from
334 // 1 to (n-1), but it would only make sense to me if it went from 0 to (n-1).
336 // This algorithm is still problematic because it can overflow left right and
339 template<typename _IntType, _IntType __m, int __s, int __r>
342 subtract_with_carry<_IntType, __m, __s, __r>::
343 seed(Gen& gen, false_type)
345 const int n = (std::numeric_limits<_IntType>::digits + 31) / 32;
346 for (int i = 0; i < long_lag; ++i)
349 unsigned long factor = 1;
350 for (int j = 0; j < n; ++j)
352 _M_x[i] += gen() * factor;
353 factor *= 0x80000000;
355 _M_x[i] = _Private::mod<_IntType, 1, 0, modulus>(_M_x[i]);
357 _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0;
361 template<typename _IntType, _IntType __m, int __s, int __r>
362 typename subtract_with_carry<_IntType, __m, __s, __r>::result_type
363 subtract_with_carry<_IntType, __m, __s, __r>::
366 // Derive short lag index from current index.
367 int ps = _M_p - short_lag;
371 // Calculate new x(i) without overflow or division.
373 if (_M_x[ps] >= _M_x[_M_p] + _M_carry)
375 xi = _M_x[ps] - _M_x[_M_p] - _M_carry;
380 xi = modulus - _M_x[_M_p] - _M_carry + _M_x[ps];
385 // Adjust current index to loop around in ring buffer.
386 if (_M_p >= long_lag)
393 template<class _E, int __p, int __r>
394 typename discard_block<_E, __p, __r>::result_type
395 discard_block<_E, __p, __r>::
398 if (_M_n >= used_block)
400 while (_M_n < block_size)
411 _GLIBCXX_END_NAMESPACE