3.8 KiB
[rand.eng.lcong]
29 Numerics library [numerics]
29.5 Random number generation [rand]
29.5.4 Random number engine class templates [rand.eng]
29.5.4.2 Class template linear_congruential_engine [rand.eng.lcong]
A linear_congruential_engine random number engine produces unsigned integer random numbers.
The state xi of a linear_congruential_engine object x is of size 1 and consists of a single integer.
The transition algorithm is a modular linear function of the formTA(xi)=(aâxi+c)modm; the generation algorithm is GA(xi)=xi+1.
namespace std {template<class UIntType, UIntType a, UIntType c, UIntType m>class linear_congruential_engine {public:// typesusing result_type = UIntType; // engine characteristicsstatic constexpr result_type multiplier = a; static constexpr result_type increment = c; static constexpr result_type modulus = m; static constexpr result_type min() { return c == 0u ? 1u: 0u; }static constexpr result_type max() { return m - 1u; }static constexpr result_type default_seed = 1u; // constructors and seeding functions linear_congruential_engine() : linear_congruential_engine(default_seed) {}explicit linear_congruential_engine(result_type s); template explicit linear_congruential_engine(Sseq& q); void seed(result_type s = default_seed); template void seed(Sseq& q); // equality operatorsfriend bool operator==(const linear_congruential_engine& x, const linear_congruential_engine& y); // generating functions result_type operator()(); void discard(unsigned long long z); // inserters and extractorstemplate<class charT, class traits>friend basic_ostream<charT, traits>&operator<<(basic_ostream<charT, traits>& os, // hostedconst linear_congruential_engine& x); template<class charT, class traits>friend basic_istream<charT, traits>&operator>>(basic_istream<charT, traits>& is, // hosted linear_congruential_engine& x); };}
If the template parameterm is 0, the modulus m used throughout [rand.eng.lcong] is numeric_limits<result_type>::max() plus 1.
[Note 1:
m need not be representable as a value of type result_type.
â end note]
If the template parameterm is not 0, the following relations shall hold: a < m and c < m.
The textual representation consists of the value of xi.
explicit linear_congruential_engine(result_type s);
Effects: If cmodm is 0 and smodm is 0, sets the engine's state to 1, otherwise sets the engine's state to smodm.
template<class Sseq> explicit linear_congruential_engine(Sseq& q);
Effects: With k=âlog2m32â and a an array (or equivalent) of length k+3, invokes q.generate(a+0, a+k+3) and then computes S=(âkâ1j=0aj+3â232j)modm.
If cmodm is 0 and S is 0, sets the engine's state to 1, else sets the engine's state to S.