📄 random
字号:
}
result_type (max)() const
{ // return maximum possible generated value
return (_Swc_Traits::_Max);
}
result_type operator()()
{ // return next value
int _Ix = 2 * _Rx <= this->_Idx ? 0 : this->_Idx;
if (_Ix < _Sx)
_Setx(_Ix, this->_Ax[_Ix + 2 * _Rx - _Sx],
this->_Ax[_Ix + _Rx]);
else if (_Ix < _Rx)
_Setx(_Ix, this->_Ax[_Ix - _Sx], this->_Ax[_Ix + _Rx]);
else
_Setx(_Ix, this->_Ax[_Ix - _Sx], this->_Ax[_Ix - _Rx]);
this->_Idx = _Ix + 1;
return (this->_Ax[_Ix]);
}
void discard(unsigned long long _Nskip)
{ // discard _Nskip elements
for (; 0 < _Nskip; --_Nskip)
(*this)();
}
bool _Equals(const _Swc_base& _Right) const
{ // return true if *this will generate same sequence as _Right
return (_Mybase::_Equals(_Right));
}
template<class _Elem, class _Traits>
_STD basic_ostream<_Elem, _Traits>&
_Write(_STD basic_ostream<_Elem, _Traits>& _Ostr) const
{ // write state to _Ostr
_Swc_Traits::_Write(_Ostr, *this, _Carry);
return (_Ostr);
}
private:
template<class _Gen>
void _Seed(_Gen& _Gx, bool _Readcy, const true_type&)
{ // reset sequence from numeric value
_RNG_ASSERT(0 < _Gx,
"invalid argument for subtract_with_carry[_01]::seed");
linear_congruential<unsigned long, 40014, 0, 2147483563> _Lc(_Gx);
_Reset(_Lc, _Readcy);
}
template<class _Gen>
void _Seed(_Gen& _Gx, bool _Readcy, const false_type&)
{ // reset sequence from generator
_Reset(_Gx, _Readcy);
}
template<class _Gen>
void _Reset(_Gen& _Gx, bool _Readcy)
{ // reset sequence
_Carry = _Swc_Traits::_Reset(_Gx, this->_Ax, _Readcy);
this->_Idx = _Rx;
}
void _Setx(int _Ix, _Ty _First, _Ty _Second)
{ // update _Ax[_Ix] and _Carry
_Ty _Newx = (_First -= _Carry) - _Second;
if (_First < _Second || _Swc_Traits::_Mod <= _Newx)
{ // underflowed, so add _Mod
_Newx += _Swc_Traits::_Mod;
_Carry = _Swc_Traits::_Cy;
}
else
_Carry = 0;
this->_Ax[_Ix] = _Newx;
}
typename _Swc_Traits::_Cy_t _Carry;
};
template<class _Ty,
int _Sx,
int _Rx,
class _Swc_Traits>
bool operator==(const _Swc_base<_Ty, _Sx, _Rx, _Swc_Traits>& _Left,
const _Swc_base<_Ty, _Sx, _Rx, _Swc_Traits>& _Right)
{ // return true if _Left will generate same sequence as _Right
return (_Left._Equals(_Right));
}
template<class _Ty,
int _Sx,
int _Rx,
class _Swc_Traits>
bool operator!=(const _Swc_base<_Ty, _Sx, _Rx, _Swc_Traits>& _Left,
const _Swc_base<_Ty, _Sx, _Rx, _Swc_Traits>& _Right)
{ // return true if _Left will not generate same sequence as _Right
return (!_Left._Equals(_Right));
}
template<class _Elem,
class _Traits,
class _Ty,
int _Sx,
int _Rx,
class _Swc_Traits>
_STD basic_istream<_Elem, _Traits>&
operator>>(_STD basic_istream<_Elem, _Traits>& _Istr,
_Swc_base<_Ty, _Sx, _Rx, _Swc_Traits>& _Eng)
{ // read state from _Istr
_Wrap_istream<_Elem, _Traits, typename _Swc_Traits::_Seed_t>
_Gen(_Istr);
_Eng.seed(_Gen, true);
return (_Istr);
}
template<class _Elem,
class _Traits,
class _Ty,
int _Sx,
int _Rx,
class _Swc_Traits>
_STD basic_ostream<_Elem, _Traits>&
operator<<(_STD basic_ostream<_Elem, _Traits>& _Ostr,
const _Swc_base<_Ty, _Sx, _Rx, _Swc_Traits>& _Eng)
{ // write state to _Ostr
return (_Eng._Write(_Ostr));
}
template<class _Ty,
int _Sx,
int _Rx,
class _Swc_Traits>
const int _Swc_base<_Ty, _Sx, _Rx, _Swc_Traits>::short_lag;
template<class _Ty,
int _Sx,
int _Rx,
class _Swc_Traits>
const int _Swc_base<_Ty, _Sx, _Rx, _Swc_Traits>::long_lag;
// TEMPLATE CLASS subtract_with_carry
template<class _Ty,
_Ty _Mx,
int _Nw>
struct _Swc_traits
{ // traits template for subtract_with_carry generator
typedef int _Cy_t;
typedef _Ty _Mod_t;
typedef _Ty _Seed_t;
static const _Cy_t _Cy = 1;
static const _Mod_t _Mod = _Mx;
static const _Ty _Max = _Mx - 1;
template<class _Gen>
static _Cy_t _Reset(_Gen& _Gx, _Ty *_Ax, bool _Readcy)
{ // set initial values of _Ax from generator _Gx
// return value of _Cy from range if _Readcy is true,
// otherwise compute from last value
int _Kx;
if (_Mx == 0)
_Kx = (_BITS_BYTE * sizeof (_Ty) + 31) / 32;
else
{ // compute number of 32-bit words required
_ULonglong _Val = (_ULonglong)1 << 32;
for (_Kx = 1; 0 < _Val && _Val < _Mx; ++_Kx)
_Val = _Val << 32;
}
for (int _Ix = 0; _Ix < _Nw; ++_Ix)
{ // pack _Kx words
_Ax[_Ix] = _Gx();
for (int _Jx = 0; ++_Jx < _Kx; )
_Ax[_Ix] |= (_Ty)_Gx() << (32 * _Jx);
}
_Cy_t _Ans = _Reduce(_Ax);
if (!_Readcy)
return (_Ans);
else
return (_Gx());
}
static _Cy_t _Reduce(_Ty *_Ax)
{ // reduce values to allowed range
if (_Mx != 0)
for (int _Ix = 0; _Ix < _Nw; ++_Ix)
_Ax[_Ix] = _Ax[_Ix] % _Mx;
return (_Ax[_Nw - 1] == 0);
}
template<class _Elem,
class _Traits>
static void _Write(_STD basic_ostream<_Elem, _Traits>& _Ostr,
const _Circ_buf<_Ty, _Nw>& _Buf, _Cy_t _Cy)
{ // write state to _Ostr
for (int _Idx = 0; _Idx < _Nw; ++_Idx)
_Ostr << _Buf._At(_Idx) << ' ';
_Ostr << _Cy;
}
};
template<class _Ty,
_Ty _Mx,
int _Sx,
int _Rx>
class subtract_with_carry
: public _Swc_base<_Ty, _Sx, _Rx, _Swc_traits<_Ty, _Mx, _Rx> >
{ // template class for subtract_with_carry generator
public:
typedef subtract_with_carry<_Ty, _Mx, _Sx, _Rx> _Myt;
typedef _Swc_base<_Ty, _Sx, _Rx, _Swc_traits<_Ty, _Mx, _Rx> > _Mybase;
static const _Ty modulus = _Mx;
static const _Ty default_seed = 19780503U;
explicit subtract_with_carry(_Ty _X0 = default_seed)
: _Mybase(_X0)
{ // construct with default seed
}
subtract_with_carry(const _Myt& _Right)
{ // construct by copying
*this = _Right;
}
subtract_with_carry(_Myt& _Right)
{ // construct by copying
*this = _Right;
}
#if _HAS_CPP0X
explicit subtract_with_carry(seed_seq& _Seq)
: _Mybase(_Seq)
{ // construct from seed sequence
}
#endif /* _HAS_CPP0X */
template<class _Gen>
subtract_with_carry(_Gen& _Gx)
: _Mybase(_Gx)
{ // construct with seed values from generator
}
};
#if _HAS_CPP0X
template<class _Ty,
int _Wx,
int _Sx,
int _Rx>
class subtract_with_carry_engine
: public subtract_with_carry<_Ty,
(_Ty(1) << (_Wx - 1)) << 1, _Sx, _Rx>
{ // template class for subtract_with_carry generator
public:
typedef subtract_with_carry_engine<_Ty, _Wx, _Sx, _Rx> _Myt;
static const _Ty _Mx = (_Ty(1) << (_Wx - 1)) << 1;
typedef subtract_with_carry<_Ty, _Mx, _Sx, _Rx> _Mybase;
static const size_t word_size = _Wx;
static const size_t short_lag = _Sx;
static const size_t long_lag = _Rx;
static const _Ty default_seed = 19780503U;
explicit subtract_with_carry_engine(_Ty _X0 = default_seed)
: _Mybase(_X0)
{ // construct with default seed
}
subtract_with_carry_engine(const _Myt& _Right)
{ // construct by copying
*this = _Right;
}
subtract_with_carry_engine(_Myt& _Right)
{ // construct by copying
*this = _Right;
}
explicit subtract_with_carry_engine(seed_seq& _Seq)
: _Mybase(_Seq)
{ // construct from seed sequence
}
template<class _Gen>
subtract_with_carry_engine(_Gen& _Gx)
: _Mybase(_Gx)
{ // construct with seed values from generator
}
static _Ty (min)()
{ // return minimum possible generated value
return (0);
}
static _Ty (max)()
{ // return maximum possible generated value
return (_Mx - 1);
}
};
#endif /* _HAS_CPP0X */
// TEMPLATE CLASS subtract_with_carry_01
template<class _Ty,
int _Wx,
int _Rx>
struct _Swc_01_traits
{ // traits template for subtract_with_carry_01 generator
typedef _Ty _Cy_t;
typedef _Ty _Mod_t;
typedef unsigned int _Seed_t;
static const _Cy_t _Cy;
static const _Mod_t _Mod;
static const _Ty _Max;
static const int _Nwords = (_Wx + 31) / 32;
template<class _Gen>
static _Cy_t _Reset(_Gen& _Gx, _Ty *_Ax, bool _Readcy)
{ // set initial values of _Ax from generator _Gx
// return value of _Cy from range if _Readcy is true,
// otherwise from last value
for (int _Ix = 0; _Ix < _Rx; ++_Ix)
{ // read values
_Ty _Factor = 1;
_Ty _Val = 0;
for (int _Jx = 0; _Jx < _Nwords - 1; ++_Jx)
{ // read components of value
_Factor /= (_Ty)_Two32;
_Val += _Gx() * _Factor;
}
_Ty _Temp = ((unsigned long)_Gx() & _Mask) / _Scale1;
_Val += (_Temp - (unsigned long)_Temp) * _Factor;
_Ax[_Ix] = _Val;
}
if (!_Readcy)
return (_Ax[_Rx - 1] != 0 ? 0 : _Cy);
else
return (_Gx() == 0 ? 0 : _Cy);
}
template<class _Elem,
class _Traits>
static void _Write(_STD basic_ostream<_Elem, _Traits>& _Ostr,
const _Circ_buf<_Ty, _Rx>& _Buf, _Cy_t _Cy)
{ // write state to _Ostr
for (int _Ix = 0; _Ix < _Rx; ++_Ix)
{ // write values
_Ty _Val = _Buf._At(_Ix);
unsigned long _Temp;
for (int _Jx = 0; _Jx < _Nwords - 1; ++_Jx)
{ // write components of value
_Val *= (_Ty)_Two32;
_Temp = (unsigned long)_Val;
_Val -= _Temp;
_Ostr << _Temp << ' ';
}
_Temp = (unsigned long)(_Val * _Scale1);
_Ostr << _Temp << ' ';
}
_Ostr << (_Cy ? 1 : 0);
}
private:
static const _Ty _Scale1;
static const unsigned long _Mask;
};
template<class _Ty,
int _Wx,
int _Rx>
const typename _Swc_01_traits<_Ty, _Wx, _Rx>::_Cy_t
_Swc_01_traits<_Ty, _Wx, _Rx>::_Cy =
(typename _Swc_01_traits<_Ty, _Wx, _Rx>::_Cy_t)
_CSTD ldexp(1.0, -_Wx);
template<class _Ty,
int _Wx,
int _Rx>
const typename _Swc_01_traits<_Ty, _Wx, _Rx>::_Mod_t
_Swc_01_traits<_Ty, _Wx, _Rx>::_Mod = 1;
template<class _Ty,
int _Wx,
int _Rx>
const _Ty _Swc_01_traits<_Ty, _Wx, _Rx>::_Max = 1;
template<class _Ty,
int _Wx,
int _Rx>
const _Ty _Swc_01_traits<_Ty, _Wx, _Rx>::_Scale1 =
(_Ty)_CSTD ldexp(1.0, _Wx % 32);
template<class _Ty,
int _Wx,
int _Rx>
const unsigned long _Swc_01_traits<_Ty, _Wx, _Rx>::_Mask =
~((~0UL) << (_Wx % 32));
template<class _Ty,
int _Wx,
int _Sx,
int _Rx>
class subtract_with_carry_01
: public _Swc_base<_Ty, _Sx, _Rx, _Swc_01_traits<_Ty, _Wx, _Rx> >
{ // template class for subtract_with_carry_01 generator
public:
static const int word_size = _Wx;
typedef _Swc_base<_Ty, _Sx, _Rx, _Swc_01_traits<_Ty, _Wx, _Rx> > _Mybase;
subtract_with_carry_01()
: _Mybase()
{ // construct with default seed
}
subtract_with_carry_01(const subtract_with_carry_01& _Right)
{ // construct by copying
*this = _Right;
}
subtract_with_carry_01(subtract_with_carry_01& _Right)
{ // construct by copying
*this = _Right;
}
explicit subtract_with_carry_01(typename _Mybase::_Seed_t _Value)
: _Mybase(_Value)
{ // construct with specified seed
}
template<class _Gen>
subtract_with_carry_01(_Gen& _Gx)
: _Mybase(_Gx)
{ // construct with seed values from generator
}
};
template<class _Ty,
int _Wx,
int _Sx,
int _Rx>
const int subtract_with_carry_01<_Ty, _Wx, _Sx, _Rx>::word_size;
// TEMPLATE CLASS mersenne_twister
template<class _Ty,
int _Wx,
int _Nx,
int _Mx,
int _Rx,
_Ty _Px,
int _Ux,
int _Sx,
_Ty _Bx,
int _Tx,
_Ty _Cx,
int _Lx>
class mersenne_twister
: public _Circ_buf<_Ty, _Nx>
{ // template class for mersenne twister generator
public:
typedef mersenne_twister<_Ty, _Wx, _Nx, _Mx, _Rx,
_Px, _Ux, _Sx, _Bx, _Tx, _Cx, _Lx> _Myt;
typedef _Ty result_type;
static const int word_size = _Wx;
static const int state_size = _Nx;
static const int shift_size = _Mx;
static const int mask_bits = _Rx;
static const _Ty parameter_a = _Px;
static const int output_u = _Ux;
static const int output_s = _Sx;
static const _Ty output_b = _Bx;
static const int output_t = _Tx;
static const _Ty output_c = _Cx;
static const int output_l = _Lx;
static const _Ty default_seed = 5489U;
explicit mersenne_twister(unsigned long _X0 = default_seed,
_Ty _Dxarg = _WMSK,
_Ty _Fxarg = (_Ty)1812433253)
: _Dxval(_Dxarg)
{ // construct with specified seed
seed(_X0, _Fxarg);
}
#if _HAS_CPP0X
explicit mersenne_twister(seed_seq& _Seq,
_Ty _Dxarg = _WMSK)
: _Dxval(_Dxarg)
{ // construct from seed sequence
seed(_Seq);
}
#endif /* _HAS_CPP0X */
mersenne_twister(const _Myt& _Right)
{ // construct by copying
*this = _Right;
}
mersenne_twister(_Myt& _Right)
{ // construct by copying
*this = _Right;
}
template<class _Gen>
explicit mersenne_twister(_Gen& _Gx)
: _Dxval(_WMSK)
{ // construct with seed values from generator
seed(_Gx);
}
void seed(unsigned long _X0 = default_seed,
_Ty _Fx = (_Ty)1812433253)
{ // set initial values from specified value
_RNG_ASSERT(0 < _X0,
"invalid argument for mersenne_twister::seed");
_Ty _Prev = this->_Ax[0] = _X0 & _WMSK;
for (int _Ix = 1; _Ix < _Nx; ++_Ix)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -