std::mersen...ister_engine(3)
π NAME π
std::mersenne_twister_engine::mersenne_twister_engine β okay so this is the constructor of the Mersenne Twister engine, and yes the name is a whole entire mouthful, we know π
This page lives in the C++ Standard Library manual, and it's gonna walk you through how a std::mersenne_twister_engine gets built, seeded, and copied. No cap, it's more math than you'd expect from a constructor, but we're gonna get through it together π«Ά
β¨ Synopsis β¨
mersenne_twister_engine() : (1) (since C++11)
mersenne_twister_engine(default_seed) {}
explicit mersenne_twister_engine( result_type value ); (2) (since C++11)
template< class SeedSeq > (3) (since C++11)
explicit mersenne_twister_engine( SeedSeq& seq );
mersenne_twister_engine( const mersenne_twister_engine& (4) (since C++11)
other );
(implicitly declared)
Constructs the pseudo-random number engine. That's the whole vibe of this page π
πΈ DESCRIPTION πΈ
So here's the thing β there are four ways to build this engine, and each one has its own little personality:
- (1) The default constructor. Just vibes, no seed argument, it does the default thing.
- (2) Constructs the engine with a seed value
value. Given2^wasp, the engine's initial state is determined as follows:- Sets
X_{-n}tovalue % p. - For each integer
iin[i - n, -1], setsX_ito[fΒ·(X_{i-1} xor (X_{i-1} rshift (w-2)))+i mod n] mod p, wherexorandrshiftstand for built-in bitwise XOR and bitwise right-shift respectively. (Yes, it's giving math class. We're doing it though π§)
- Sets
- (3) Constructs the engine with a seed sequence
seq. Givenstd::size_t(w / 32) + 1ask, the engine's initial state is determined as follows:- Creates an invented array object
aof lengthn * k. - Calls
seq.generate(a + 0, a + n * k). - For each integer
iin[-n, -1], setsX_ito(β^{k-1}_{j=0} a_{k(i+n)+j} Β· 2^{32j}) mod 2^w. - If the most significant
w β rbits ofX_{-n}are zero, and if each of the other resultingX_iis0, changesX_{-n}to2^{w-1}.
- Creates an invented array object
Bestie, listen β this overload participates in overload resolution only ifSeedSeqmeets the requirements ofSeedSequence. That's the condition, we don't make the rules π«
- (4) The copy constructor. Upon construction,
*this == otheristrue. Iconic, honestly β it just works π
π¦ PARAMETERS π¦
valueβ seed value to use in the initialization of the internal stateseqβ seed sequence to use in the initialization of the internal state
π₯ COMPLEXITY π₯
- 1, 2)
O(n). - 3) Same as the complexity of the
seq.generatecall. - 4)
O(n).
π DEFECT REPORTS π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. (Retroactive glow-ups, we love to see it β¨)
P0935R0β Applied to:C++11β Behavior as published: the default constructor wasexplicitβ Correct behavior: madeimplicit.
β SEE ALSO β
seedβ sets the current state of the engine β(C++11)(public member function)
Okay that's the whole page, you did amazing, go seed something responsibly π
Source/version + date check: straight from http://cppreference.com, version 2024.06.10, filed under std::mersen...ister_engine(3). Mwah π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms