📄 random
字号:
_Prev = this->_Ax[_Ix] =
(_Ix + _Fx * (_Prev ^ (_Prev >> (_Wx - 2)))) & _WMSK;
this->_Idx = _Nx;
}
#if _HAS_CPP0X
void seed(seed_seq& _Seq)
{ // reset sequence from seed sequence
static const int _Kx = (_Wx + 31) / 32;
unsigned long _Arr[_Kx * _Nx];
_Seq.generate(&_Arr[0], &_Arr[_Kx * _Nx]);
int _Idx0 = 0;
_Ty _Sum = 0;
for (int _Ix = 0; _Ix < _Nx; ++_Ix, _Idx0 += _Kx)
{ // pack _Kx words
this->_Ax[_Ix] = _Arr[_Idx0];
for (int _Jx = 0; ++_Jx < _Kx; )
this->_Ax[_Ix] |= (_Ty)_Arr[_Idx0 + _Jx] << (32 * _Jx);
this->_Ax[_Ix] &= _WMSK;
if (_Ix == 0)
_Sum = this->_Ax[_Ix] >> _Rx;
else
_Sum |= this->_Ax[_Ix];
}
if (_Sum == 0)
this->_Ax[0] = _WMSK;
this->_Idx = _Nx;
}
#endif /* _HAS_CPP0X */
template<class _Gen>
void seed(_Gen& _Gx, bool = false)
{ // set initial values from range
for (int _Ix = 0; _Ix < _Nx; ++_Ix)
this->_Ax[_Ix] = _Gx() & _WMSK;
this->_Idx = _Nx;
}
template<class _Elem,
class _S_Traits>
_STD basic_ostream<_Elem, _S_Traits>&
_Write(_STD basic_ostream<_Elem, _S_Traits>& _Ostr) const
{ // write state to _Ostr
for (int _Ix = 0; _Ix < _Nx; ++_Ix)
_Ostr << this->_At(_Ix) << ' ';
return (_Ostr);
}
result_type (min)() const
{ // return minimum possible generated value
return (0);
}
result_type (max)() const
{ // return maximum possible generated value
return (_WMSK);
}
result_type operator()()
{ // return next value
if (this->_Idx == _Nx)
_Refill_upper();
else if (2 * _Nx <= this->_Idx)
_Refill_lower();
_Ty _Res = this->_Ax[this->_Idx++] & _WMSK;
_Res ^= (_Res >> _Ux) & _Dxval;
_Res ^= (_Res << _Sx) & _Bx;
_Res ^= (_Res << _Tx) & _Cx;
_Res ^= (_Res & _WMSK) >> _Lx;
return (_Res);
}
void discard(unsigned long long _Nskip)
{ // discard _Nskip elements
for (; 0 < _Nskip; --_Nskip)
(*this)();
}
private:
void _Refill_lower()
{ // compute values for the lower half of the history array
int _Ix;
for (_Ix = 0; _Ix < _Nx - _Mx; ++_Ix)
{ // fill in lower region
_Ty _Tmp = (this->_Ax[_Ix + _Nx] & _HMSK)
| (this->_Ax[_Ix + _Nx + 1] & _LMSK);
this->_Ax[_Ix] = (_Tmp >> 1)
^ (_Tmp & 1 ? _Px : 0) ^ this->_Ax[_Ix + _Nx + _Mx];
}
for (; _Ix < _Nx - 1; ++_Ix)
{ // fill in upper region (avoids modulus operation)
_Ty _Tmp = (this->_Ax[_Ix +_Nx] & _HMSK)
| (this->_Ax[_Ix + _Nx + 1] & _LMSK);
this->_Ax[_Ix] = (_Tmp >> 1)
^ (_Tmp & 1 ? _Px : 0) ^ this->_Ax[_Ix - _Nx + _Mx];
}
_Ty _Tmp = (this->_Ax[_Ix + _Nx] & _HMSK) | (this->_Ax[0] & _LMSK);
this->_Ax[_Ix] = (_Tmp >> 1)
^ (_Tmp & 1 ? _Px : 0) ^ this->_Ax[_Mx - 1];
this->_Idx = 0;
}
void _Refill_upper()
{ // compute values for the upper half of the history array
int _Ix;
for (_Ix = _Nx; _Ix < 2 * _Nx; ++_Ix)
{ // fill in values
_Ty _Tmp = (this->_Ax[_Ix - _Nx] & _HMSK)
| (this->_Ax[_Ix - _Nx + 1] & _LMSK);
this->_Ax[_Ix] = (_Tmp >> 1)
^ (_Tmp & 1 ? _Px : 0) ^ this->_Ax[_Ix - _Nx + _Mx];
}
}
_Ty _Dxval;
static const _Ty _WMSK = ~((~_Ty(0) << (_Wx - 1)) << 1);
static const _Ty _HMSK = (_WMSK << _Rx) & _WMSK;
static const _Ty _LMSK = ~_HMSK & _WMSK;
};
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>
bool operator==(
const mersenne_twister<_Ty, _Wx, _Nx, _Mx, _Rx,
_Px, _Ux, _Sx, _Bx, _Tx, _Cx, _Lx>& _Left,
const mersenne_twister<_Ty, _Wx, _Nx, _Mx, _Rx,
_Px, _Ux, _Sx, _Bx, _Tx, _Cx, _Lx>& _Right)
{ // return true if _Left will generate same sequence as _Right
return (_Left._Equals(_Right));
}
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>
bool operator!=(
const mersenne_twister<_Ty, _Wx, _Nx, _Mx, _Rx,
_Px, _Ux, _Sx, _Bx, _Tx, _Cx, _Lx>& _Left,
const mersenne_twister<_Ty, _Wx, _Nx, _Mx, _Rx,
_Px, _Ux, _Sx, _Bx, _Tx, _Cx, _Lx>& _Right)
{ // return true if _Left will not generate same sequence as _Right
return (!_Left._Equals(_Right));
}
template<class _Elem, class _S_Traits,
class _Ty, int _Wx, int _Nx, int _Mx, int _Rx,
_Ty _Px, int _Ux, int _Sx, _Ty _Bx, int _Tx, _Ty _Cx, int _Lx>
_STD basic_istream<_Elem, _S_Traits>& operator>>(
_STD basic_istream<_Elem, _S_Traits>& _Istr,
mersenne_twister<_Ty, _Wx, _Nx, _Mx, _Rx,
_Px, _Ux, _Sx, _Bx, _Tx, _Cx, _Lx>& _Eng)
{ // read state from _Istr
_Wrap_istream<_Elem, _S_Traits, _Ty> _Gen(_Istr);
_Eng.seed(_Gen);
return (_Istr);
}
template<class _Elem, class _S_Traits,
class _Ty, int _Wx, int _Nx, int _Mx, int _Rx,
_Ty _Px, int _Ux, int _Sx, _Ty _Bx, int _Tx, _Ty _Cx, int _Lx>
_STD basic_ostream<_Elem, _S_Traits>& operator<<(
_STD basic_ostream<_Elem, _S_Traits>& _Ostr,
const mersenne_twister<_Ty, _Wx, _Nx, _Mx, _Rx,
_Px, _Ux, _Sx, _Bx, _Tx, _Cx, _Lx>& _Eng)
{ // write state to _Ostr
return (_Eng._Write(_Ostr));
}
#if _HAS_CPP0X
// TEMPLATE CLASS mersenne_twister_engine
template<class _Ty,
size_t _Wx,
size_t _Nx,
size_t _Mx,
size_t _Rx,
_Ty _Px,
size_t _Ux,
_Ty _Dx, // added
size_t _Sx,
_Ty _Bx,
size_t _Tx,
_Ty _Cx,
size_t _Lx,
_Ty _Fx> // added
class mersenne_twister_engine
: public mersenne_twister<_Ty, _Wx, _Nx, _Mx, _Rx,
_Px, _Ux, _Sx, _Bx, _Tx, _Cx, _Lx>
{ // template class for mersenne twister generator
public:
typedef mersenne_twister_engine<_Ty, _Wx, _Nx, _Mx, _Rx,
_Px, _Ux, _Dx, _Sx, _Bx, _Tx, _Cx, _Lx, _Fx> _Myt;
typedef mersenne_twister<_Ty, _Wx, _Nx, _Mx, _Rx,
_Px, _Ux, _Sx, _Bx, _Tx, _Cx, _Lx> _Mybase;
typedef _Ty result_type;
static const size_t word_size = _Wx;
static const size_t state_size = _Nx;
static const size_t shift_size = _Mx;
static const size_t mask_bits = _Rx;
static const _Ty xor_mask = _Px;
static const size_t tempering_u = _Ux;
static const _Ty tempering_d = _Dx;
static const size_t tempering_s = _Sx;
static const _Ty tempering_b = _Bx;
static const size_t tempering_t = _Tx;
static const _Ty tempering_c = _Cx;
static const size_t tempering_l = _Lx;
static const _Ty initialization_multiplier = _Fx;
static const result_type default_seed = 5489U;
mersenne_twister_engine(const _Myt& _Right)
: _Mybase((const _Mybase&)_Right)
{ // construct by copying (compiler bug workaround)
}
explicit mersenne_twister_engine(result_type _X0 = default_seed)
: _Mybase((unsigned long)_X0, _Dx, _Fx)
{ // construct with default seed
}
explicit mersenne_twister_engine(seed_seq& _Seq)
: _Mybase(_Seq, _Dx)
{ // construct from seed sequence
}
};
#endif /* _HAS_CPP0X */
// TEMPLATE CLASS discard_block
template<class _Engine,
int _Px,
int _Rx>
class discard_block
{ // template class for discard_block compound engine
public:
typedef discard_block<_Engine, _Px, _Rx> _Myt;
typedef _Engine base_type;
typedef typename _Engine::result_type result_type;
static const int block_size = _Px;
static const int used_block = _Rx;
discard_block()
: _Nx(0)
{ // construct
}
discard_block(const discard_block& _Right)
: _Eng(_Right._Eng), _Nx(_Right._Nx)
{ // construct by copying
}
discard_block(discard_block& _Right)
: _Eng(_Right._Eng), _Nx(_Right._Nx)
{ // construct by copying
}
explicit discard_block(const base_type& _Ex)
: _Eng(_Ex), _Nx(0)
{ // construct with engine initializer _Ex
}
explicit discard_block(result_type _Seed)
: _Eng(_Seed), _Nx(0)
{ // construct from specified seed value
}
#if _HAS_CPP0X
explicit discard_block(seed_seq& _Seq)
: _Eng(_Seq), _Nx(0)
{ // construct from seed sequence
}
#endif /* _HAS_CPP0X */
template<class _Gen>
discard_block(_Gen& _Gx)
: _Eng(_Gx), _Nx(0)
{ // construct from range
}
void seed()
{ // seed engine from default value
_Eng.seed();
_Nx = 0;
}
void seed(result_type _X0)
{ // seed engine from specified value
_Eng.seed(_X0);
_Nx = 0;
}
#if _HAS_CPP0X
void seed(seed_seq& _Seq)
{ // seed engine from seed sequence
_Eng.seed(_Seq);
_Nx = 0;
}
#endif /* _HAS_CPP0X */
template<class _Gen>
void seed(_Gen& _Gx, bool _Readcy = false)
{ // seed engine from range
_Eng.seed(_Gx, _Readcy);
_Nx = 0;
}
const base_type& base() const
{ // return const reference to engine
return (_Eng);
}
result_type (min)() const
{ // return minimum possible generated value
return ((_Eng.min)());
}
result_type (max)() const
{ // return maximum possible generated value
return ((_Eng.max)());
}
result_type operator()()
{ // return next value
if (_Rx <= _Nx)
{ // discard values
while (_Nx++ < _Px)
_Eng();
_Nx = 0;
}
++_Nx;
return (_Eng());
}
void discard(unsigned long long _Nskip)
{ // discard _Nskip elements
for (; 0 < _Nskip; --_Nskip)
(*this)();
}
bool _Equals(const _Myt& _Right) const
{ // return true if *this will generate same sequence as _Right
return (_Eng == _Right._Eng && _Nx == _Right._Nx);
}
template<class _Elem,
class _Traits>
_STD basic_istream<_Elem, _Traits>& _Read(
_STD basic_istream<_Elem, _Traits>& _Istr)
{ // read state from _Istr
return (_Istr >> _Eng >> _Nx);
}
template<class _Elem,
class _Traits>
_STD basic_ostream<_Elem, _Traits>& _Write(
_STD basic_ostream<_Elem, _Traits>& _Ostr) const
{ // write state to _Ostr
return (_Ostr << _Eng << ' ' << _Nx);
}
private:
base_type _Eng;
int _Nx;
};
template<class _Engine,
int _Px,
int _Rx>
const int discard_block<_Engine, _Px, _Rx>::block_size;
template<class _Engine,
int _Px,
int _Rx>
const int discard_block<_Engine, _Px, _Rx>::used_block;
template<class _Engine,
int _Px,
int _Rx>
bool operator==(
const discard_block<_Engine, _Px, _Rx>& _Left,
const discard_block<_Engine, _Px, _Rx>& _Right)
{ // return true if _Left will generate same sequence as _Right
return (_Left._Equals(_Right));
}
template<class _Engine,
int _Px,
int _Rx>
bool operator!=(
const discard_block<_Engine, _Px, _Rx>& _Left,
const discard_block<_Engine, _Px, _Rx>& _Right)
{ // return true if _Left will not generate same sequence as _Right
return (!(_Left == _Right));
}
template<class _Elem,
class _Traits,
class _Engine,
int _Px,
int _Rx>
_STD basic_istream<_Elem, _Traits>& operator>>(
_STD basic_istream<_Elem, _Traits>& _Istr,
discard_block<_Engine, _Px, _Rx>& _Eng)
{ // read state from _Istr
return (_Eng._Read(_Istr));
}
template<class _Elem,
class _Traits,
class _Engine,
int _Px,
int _Rx>
_STD basic_ostream<_Elem, _Traits>& operator<<(
_STD basic_ostream<_Elem, _Traits>& _Ostr,
const discard_block<_Engine, _Px, _Rx>& _Eng)
{ // write state to _Ostr
return (_Eng._Write(_Ostr));
}
#if _HAS_CPP0X
// TEMPLATE CLASS discard_block_engine
template<class _Engine,
int _Px,
int _Rx>
class discard_block_engine
: public discard_block<_Engine, _Px, _Rx>
{ // template class for discard_block_engine compound engine
public:
typedef discard_block_engine<_Engine, _Px, _Rx> _Myt;
typedef discard_block<_Engine, _Px, _Rx> _Mybase;
typedef _Engine base_type;
typedef typename _Engine::result_type result_type;
discard_block_engine()
{ // construct
}
discard_block_engine(const _Myt& _Right)
: _Mybase((const _Mybase&)_Right)
{ // construct by copying (compiler bug workaround)
}
explicit discard_block_engine(const base_type& _Ex)
: _Mybase(_Ex)
{ // construct with engine initializer _Ex
}
explicit discard_block_engine(result_type _X0)
: _Mybase(_X0)
{ // construct from specified seed value
}
explicit discard_block_engine(seed_seq& _Seq)
: _Mybase(_Seq)
{ // construct from seed sequence
}
static typename _Engine::result_type (min)()
{ // return minimum possible generated value
return ((_Engine::min)());
}
static typename _Engine::result_type (max)()
{ // return maximum possible generated value
return ((_Engine::max)());
}
};
// TEMPLATE CLASS independent_bits_engine
template<class _Engine,
size_t _Wx,
class _UIntType>
class independent_bits_engine
{ // template class for independent_bits_engine compound engine
public:
typedef independent_bits_engine<_Engine, _Wx, _UIntType> _Myt;
typedef _Engine base_type;
typedef _UIntType result_type;
typedef typename _Engine::result_type _Eres;
independent_bits_engine()
{ // construct
_Init();
}
explicit independent_bits_engine(const base_type& _Ex)
: _Eng(_Ex)
{ // construct with engine initializer _Ex
_Init();
}
explicit independent_bits_engine(result_type _X0)
: _Eng(_X0)
{ // construct from specified seed value
_Init();
}
explicit independent_bits_engine(seed_seq& _Seq)
: _Eng(_Seq)
{ // construct from seed sequence
_Init();
}
void seed()
{ // seed engine from default value
_Eng.seed();
}
void seed(result_type _X0)
{ // seed engine from specified value
_Eng.seed(_X0);
}
void seed(seed_seq& _Seq)
{ // seed engine from seed sequence
_Eng.seed(_Seq);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -