⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 random

📁 C语言库函数的原型,有用的拿去
💻
📖 第 1 页 / 共 5 页
字号:
		}

	const base_type& base() const
		{	// return const reference to engine
		return (_Eng);
		}

	static result_type (min)()
		{	// return minimum possible generated value
		return (0);
		}

	static result_type (max)()
		{	// return maximum possible generated value
		return ((((result_type)1 << (_Wx - 1)) << 1) - 1);
		}

	result_type operator()()
		{	// return next value
		size_t _Idx = 0;
		result_type _Res = 0;
		result_type _Mask = (((result_type)1 << (_W0 - 1)) << 1) - 1;
		_Eres _Val;

		for (; _Idx < _N0; ++_Idx)
			{	// pack _W0-bit values
			for (; ; )
				{	// get a small enough value
				_Val = _Eng() -  (_Eng.min)();
				if (_Val <= _Y0)
					break;
				}
			_Res = _Res << _W0 | _Val & _Mask;
			}

		_Mask = _Mask << 1 | 1;
		for (; _Idx < _Nx; ++_Idx)
			{	// pack _W0+1-bit values
			for (; ; )
				{	// get a small enough value
				_Val = _Eng() -  (_Eng.min)();
				if (_Val <= _Y1)
					break;
				}
			_Res = (_Res << (_W0 + 1)) | _Val & _Mask;
			}
		return (_Res);
		}

	void discard(unsigned long long _Nskip)
		{	// discard _Nskip elements
		for (; 0 < _Nskip; --_Nskip)
			(*this)();
		}

	template<class _Elem,
		class _Traits>
		_STD basic_istream<_Elem, _Traits>& _Read(
			_STD basic_istream<_Elem, _Traits>& _Istr)
		{	// read state from _Istr
		return (_Istr >> _Eng);
		}

	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);
		}

private:
	void _Init()
		{	// compute values for operator()
		size_t _Mx = 0;
		_Eres _Rx = (_Eng.max)() -  (_Eng.min)() + 1;

		_Eres _Tmp = _Rx;
		if (_Tmp == 0)
			{	// all bits used, make _Rx finite
			_Mx = 1;
			--_Tmp;
			}
		for (; 1 < _Tmp; _Tmp >>= 1)
			++_Mx;	// compute _Mx = floor(log2(_Rx))

		for (size_t _Nfix = 0; ; ++_Nfix)
			{	// compute consistent set of parameters
			_Nx = (_Wx + _Mx - 1) / _Mx + _Nfix;	// trial _Nx
			_W0 = _Wx / _Nx;
			_N0 = _Nx - _Wx % _Nx;
			_Y0 = (_Rx >> _W0) << _W0;
			_Y1 = (((_Rx >> _W0) >> 1) << _W0) << 1;
			if (_Nfix == 1 || _Rx - _Y0 <= _Y0 / _Nx)
				break;	// also works if _Rx == 0 (_Mx == all bits)
			}
		--_Y0;
		--_Y1;
		}

	base_type _Eng;	// the stored engine
	size_t _N0;		// nuber of smaller packing words
	size_t _Nx;		// total number of packing words
	size_t _W0;		// bits per smaller packing word
	_Eres _Y0;		// max value for smaller packing word
	_Eres _Y1;		// max value for larger packing word
	};

template<class _Engine,
	size_t _Wx,
	class _UIntType>
	bool operator==(
		const independent_bits_engine<_Engine, _Wx, _UIntType>& _Left,
		const independent_bits_engine<_Engine, _Wx, _UIntType>& _Right)
	{	// return true if _Left will generate same sequence as _Right
	return (_Left.base() == _Right.base());
	}

template<class _Engine,
	size_t _Wx,
	class _UIntType>
	bool operator!=(
		const independent_bits_engine<_Engine, _Wx, _UIntType>& _Left,
		const independent_bits_engine<_Engine, _Wx, _UIntType>& _Right)
	{	// return true if _Left will not generate same sequence as _Right
	return (!(_Left == _Right));
	}

template<class _Elem,
	class _Traits,
	class _Engine,
	size_t _Wx,
	class _UIntType>
	_STD basic_istream<_Elem, _Traits>& operator>>(
		_STD basic_istream<_Elem, _Traits>& _Istr,
		independent_bits_engine<_Engine, _Wx, _UIntType>& _Eng)
	{	// read state from _Istr
	return (_Eng._Read(_Istr));
	}

template<class _Elem,
	class _Traits,
	class _Engine,
	size_t _Wx,
	class _UIntType>
	_STD basic_ostream<_Elem, _Traits>& operator<<(
		_STD basic_ostream<_Elem, _Traits>& _Ostr,
		const independent_bits_engine<_Engine, _Wx, _UIntType>& _Eng)
	{	// write state to _Ostr
	return (_Eng._Write(_Ostr));
	}

	// TEMPLATE CLASS shuffle_order_engine
template<class _Engine,
	size_t _Kx>
	class shuffle_order_engine
	{	// template class for shuffle_order_engine compound engine
public:
	typedef shuffle_order_engine<_Engine, _Kx> _Myt;
	typedef _Engine base_type;
	typedef typename _Engine::result_type result_type;

	static const size_t table_size = _Kx;

	shuffle_order_engine()
		{	// construct
		_Init();
		}

	explicit shuffle_order_engine(const base_type& _Ex)
		: _Eng(_Ex)
		{	// construct with engine initializer _Ex
		_Init();
		}

	explicit shuffle_order_engine(result_type _X0)
		: _Eng(_X0)
		{	// construct from specified seed value
		_Init();
		}

	explicit shuffle_order_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);
		}

	const base_type& base() const
		{	// return const reference to engine
		return (_Eng);
		}

	static result_type (min)()
		{	// return minimum possible generated value
		return ((_Engine::min)());
		}

	static result_type (max)()
		{	// return maximum possible generated value
		return ((_Engine::max)());
		}

	result_type operator()()
		{	// return next value
		size_t _Idx = (size_t)((double)(_Yx -  (_Eng.min)()) * _Scale);

		_Yx = _Arr[_Idx];
		_Arr[_Idx] = _Eng();
		return (_Yx);
		}

	void discard(unsigned long long _Nskip)
		{	// discard _Nskip elements
		for (; 0 < _Nskip; --_Nskip)
			(*this)();
		}

	template<class _Elem,
		class _Traits>
		_STD basic_istream<_Elem, _Traits>& _Read(
			_STD basic_istream<_Elem, _Traits>& _Istr)
		{	// read state from _Istr
		_Istr >> _Eng;
		for (size_t _Idx = 0; _Idx < _Kx; ++_Idx)
			_Istr >> _Arr[_Idx];
		return (_Istr >> _Yx);
		}

	template<class _Elem,
		class _Traits>
		_STD basic_ostream<_Elem, _Traits>& _Write(
			_STD basic_ostream<_Elem, _Traits>& _Ostr) const
		{	// write state to _Ostr
		_Ostr << _Eng;
		for (size_t _Idx = 0; _Idx < _Kx; ++_Idx)
			_Ostr << ' ' << _Arr[_Idx];
		return (_Ostr << ' ' << _Yx);
		}

private:
	void _Init()
		{	// compute values for operator()
		for (size_t _Idx = 0; _Idx < _Kx; ++_Idx)
			_Arr[_Idx] = _Eng();
		_Yx = _Eng();
		_Scale = (double)_Kx
			/ ((double)(_Eng.max)() - (double)(_Eng.min)() + 1.0);
		}

	base_type _Eng;	// the stored engine
	result_type _Arr[_Kx];
	result_type _Yx;
	double _Scale;
	};

template<class _Engine,
	size_t _Kx>
	bool operator==(
		const shuffle_order_engine<_Engine, _Kx>& _Left,
		const shuffle_order_engine<_Engine, _Kx>& _Right)
	{	// return true if _Left will generate same sequence as _Right
	return (_Left.base() == _Right.base());
	}

template<class _Engine,
	size_t _Kx>
	bool operator!=(
		const shuffle_order_engine<_Engine, _Kx>& _Left,
		const shuffle_order_engine<_Engine, _Kx>& _Right)
	{	// return true if _Left will not generate same sequence as _Right
	return (!(_Left == _Right));
	}

template<class _Elem,
	class _Traits,
	class _Engine,
	size_t _Kx>
	_STD basic_istream<_Elem, _Traits>& operator>>(
		_STD basic_istream<_Elem, _Traits>& _Istr,
		shuffle_order_engine<_Engine, _Kx>& _Eng)
	{	// read state from _Istr
	return (_Eng._Read(_Istr));
	}

template<class _Elem,
	class _Traits,
	class _Engine,
	size_t _Kx>
	_STD basic_ostream<_Elem, _Traits>& operator<<(
		_STD basic_ostream<_Elem, _Traits>& _Ostr,
		const shuffle_order_engine<_Engine, _Kx>& _Eng)
	{	// write state to _Ostr
	return (_Eng._Write(_Ostr));
	}
 #endif /* _HAS_CPP0X */

	// TEMPLATE CLASS xor_combine
template<class _Engine1,
	int _S1,
	class _Engine2,
	int _S2>
	class xor_combine
	{	// template class for xor_combine compound engine
public:
	typedef xor_combine<_Engine1, _S1, _Engine2, _S2> _Myt;
	typedef _Engine1 base1_type;
	typedef _Engine2 base2_type;

	typedef typename _Engine1::result_type _Eres1;
	typedef typename _Engine1::result_type _Eres2;
	typedef typename _If<(_Eres1)(-1) <= (_Eres2)(-1),
		_Eres1, _Eres2>::_Type result_type;

	static const int shift1 = _S1;
	static const int shift2 = _S2;

	xor_combine()
		{	// construct
		_Init();
		}

	xor_combine(const xor_combine& _Right)
		: _Eng1(_Right._Eng1), _Eng2(_Right._Eng2),
			_Min(_Right._Min), _Max(_Right._Max)
		{	// construct by copying
		}

	xor_combine(xor_combine& _Right)
		: _Eng1(_Right._Eng1), _Eng2(_Right._Eng2),
			_Min(_Right._Min), _Max(_Right._Max)
		{	// construct by copying
		}

	explicit xor_combine(result_type _X0)
		: _Eng1((_Eres1)_X0), _Eng2((_Eres2)_X0 + 1)
		{	// construct with common seed
		_Init();
		}

 #if _HAS_CPP0X
	explicit xor_combine(seed_seq& _Seq)
		: _Eng1(_Seq), _Eng2(_Seq)
		{	// construct from seed sequence
		}
 #endif /* _HAS_CPP0X */

	xor_combine(const base1_type& _E1, const base2_type& _E2)
		: _Eng1(_E1), _Eng2(_E2)
		{	// construct with engine initializers _E1 and _E2
		_Init();
		}

	template<class _Gen>
		xor_combine(_Gen& _Gx)
		: _Eng1(_Gx), _Eng2(_Gx)
		{	// construct from range
		_Init();
		}

	void seed()
		{	// seed from default values
		_Eng1.seed();
		_Eng2.seed();
		}

	void seed(result_type _X0)
		{	// seed from default values
		_Eng1.seed((_Eres1)_X0);
		_Eng2.seed((_Eres2)_X0 + 1);
		}

 #if _HAS_CPP0X
	void seed(seed_seq& _Seq)
		{	// reset sequence from seed sequence
		_Eng1.seed(_Seq);
		_Eng2.seed(_Seq);
		}
 #endif /* _HAS_CPP0X */

	template<class _Gen>
		void seed(_Gen& _Gx, bool = false)
		{	// seed from generator
		_Eng1.seed(_Gx);
		_Eng2.seed(_Gx);
		}

	const base1_type& base1() const
		{	// return const reference to first engine
		return (_Eng1);
		}

	const base2_type& base2() const
		{	// return const reference to second engine
		return (_Eng2);
		}

	base1_type& _Base1()
		{	// return reference to first engine
		return (_Eng1);
		}

	base2_type& _Base2()
		{	// return reference to second engine
		return (_Eng2);
		}

	result_type (min)() const
		{	// return minimum possible generated value
		return (_Min);
		}

	result_type (max)() const
		{	// return maximum possible generated value
		return (_Max);
		}

	result_type operator()()
		{	// return next value
		return ((_Eng1() << _S1) ^ (_Eng2() << _S2));
		}

	void discard(unsigned long long _Nskip)
		{	// discard _Nskip elements
		for (; 0 < _Nskip; --_Nskip)
			(*this)();
		}

private:
	void _Init()
		{	// determine minimum and maximum values
		result_type _Mx, _Temp;
		result_type _Ax, _Bx, _Cx, _Dx;
		unsigned _SS = _S1 < _S2 ? _S1 : _S2;

		_Mx = (_STD numeric_limits<result_type>::max)()
			- ((_STD numeric_limits<result_type>::max)() >> 1);
		_Ax = (_Eng1.min)() << (_S1 - _SS);
		_Bx = (_Eng1.max)() << (_S1 - _SS);
		_Cx = (_Eng2.min)() << (_S2 - _SS);
		_Dx = (_Eng2.max)() << (_S2 - _SS);
		while (_Mx != 0)
			{	// loop through bits
			if (_Mx & ~_Ax & _Cx)
				{	// adjust minimum from _Eng1
				if ((_Temp = (_Ax | _Mx) & (0 - _Mx)) <= _Bx)
					_Ax = _Temp;
				}
			else if (_Mx & _Ax & ~_Cx)
				{	// adjust minimum from _Eng2
				if ((_Temp = (_Cx | _Mx) & (0 - _Mx)) <= _Dx)
					_Cx = _Temp;
				}
			_Mx >>= 1;
			}
		_Min = (_Ax ^ _Cx) << _SS;

		_Mx = (_STD numeric_limits<result_type>::max)()
			- ((_STD numeric_limits<result_type>::max)() >> 1);
		_Ax = (_Eng1.min)() << (_S1 - _SS);
		_Bx = (_Eng1.max)() << (_S1 - _SS);
		_Cx = (_Eng2.min)() << (_S2 - _SS);
		_Dx = (_Eng2.max)() << (_S2 - _SS);
		while (_Mx != 0)
			{	// loop through bits
			if (!(_Mx & _Bx & _Dx))
				;
			else if (_Ax <= (_Temp = (_Bx - _Mx) | (_Mx - 1)))
				_Bx = _Temp;
			else if (_Cx <= (_Temp = (_Dx - _Mx) | (_Mx - 1)))
				_Dx = _Temp;
			_Mx >>= 1;
			}
		_Max = (_Bx ^ _Dx) << _SS;
		}

	_Engine1 _Eng1;
	_Engine2 _Eng2;
	result_type _Min;
	result_type _Max;
	};

template<class _Engine1,
	int _S1,
	class _Engine2,
	int _S2>
	const int xor_combine<_Engine1, _S1, _Engine2, _S2>::shift1;

template<class _Engine1,
	int _S1,
	class _Engine2,
	int _S2>
	const int xor_combine<_Engine1, _S1, _Engine2, _S2>::shift2;

template<class _Engine1,
	int _S1,
	class _Engine2,
	int _S2>
	bool operator==(
		const xor_combine<_Engine1, _S1, _Engine2, _S2>& _Left,
		const xor_combine<_Engine1, _S1, _Engine2, _S2>& _Right)
	{	// return true if _Left will generate same sequence as _Right
	return (_Left.base1() == _Right.base1()
		&& _Left.ba

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -