[rand.eng.lcong] # 29 Numerics library [[numerics]](./#numerics) ## 29.5 Random number generation [[rand]](rand#eng.lcong) ### 29.5.4 Random number engine class templates [[rand.eng]](rand.eng#lcong) #### 29.5.4.2 Class template linear_congruential_engine [rand.eng.lcong] [1](#1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L2670) A linear_congruential_engine random number engine produces unsigned integer random numbers[.](#1.sentence-1) The state xi of a linear_congruential_engine object x is of size 1 and consists of a single integer[.](#1.sentence-2) The transition algorithm is a modular linear function of the formTA(xi)=(aâ‹xi+c)modm; the generation algorithm is GA(xi)=xi+1[.](#1.sentence-3) [🔗](#lib:linear_congruential_engine_) namespace std {templateclass 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 extractorstemplatefriend basic_ostream&operator<<(basic_ostream& os, // hostedconst linear_congruential_engine& x); templatefriend basic_istream&operator>>(basic_istream& is, // hosted linear_congruential_engine& x); };} [2](#2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L2729) If the template parameterm is 0, the modulus m used throughout [rand.eng.lcong] is numeric_limits​::​max() plus 1[.](#2.sentence-1) [*Note [1](#note-1)*: m need not be representable as a value of type result_type[.](#2.sentence-2) — *end note*] [3](#3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L2740) If the template parameterm is not 0, the following relations shall hold: a < m and c < m[.](#3.sentence-1) [4](#4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L2748) The textual representation consists of the value of xi[.](#4.sentence-1) [🔗](#lib:linear_congruential_engine,constructor) `explicit linear_congruential_engine(result_type s); ` [5](#5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L2759) *Effects*: If cmodm is 0 and smodm is 0, sets the engine's state to 1, otherwise sets the engine's state to smodm[.](#5.sentence-1) [🔗](#lib:linear_congruential_engine,constructor_) `template explicit linear_congruential_engine(Sseq& q); ` [6](#6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L2772) *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[.](#6.sentence-1) If cmodm is 0 and S is 0, sets the engine's state to 1, else sets the engine's state to S[.](#6.sentence-2)