std::mersen...ister_engine(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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. Given 2^w as p, the engine's initial state is determined as follows:
    1. Sets X_{-n} to value % p.
    2. For each integer i in [i - n, -1], sets X_i to [f·(X_{i-1} xor (X_{i-1} rshift (w-2)))+i mod n] mod p, where xor and rshift stand for built-in bitwise XOR and bitwise right-shift respectively. (Yes, it's giving math class. We're doing it though 🧚)
  • (3) Constructs the engine with a seed sequence seq. Given std::size_t(w / 32) + 1 as k, the engine's initial state is determined as follows:
    1. Creates an invented array object a of length n * k.
    2. Calls seq.generate(a + 0, a + n * k).
    3. For each integer i in [-n, -1], sets X_i to (βˆ‘^{k-1}_{j=0} a_{k(i+n)+j} Β· 2^{32j}) mod 2^w.
    4. If the most significant w βˆ’ r bits of X_{-n} are zero, and if each of the other resulting X_i is 0, changes X_{-n} to 2^{w-1}.
Bestie, listen β€” this overload participates in overload resolution only if SeedSeq meets the requirements of SeedSequence. That's the condition, we don't make the rules 🫠
  • (4) The copy constructor. Upon construction, *this == other is true. Iconic, honestly β€” it just works πŸ‘‘

πŸ¦‹ PARAMETERS πŸ¦‹

  • value β€” seed value to use in the initialization of the internal state
  • seq β€” seed sequence to use in the initialization of the internal state

πŸ”₯ COMPLEXITY πŸ”₯

  • 1, 2) O(n).
  • 3) Same as the complexity of the seq.generate call.
  • 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 was explicit β€” Correct behavior: made implicit.

⭐ 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