Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Mersenne Twister is a shift-register based pRNG (pseudo-random number generator) and is therefore subject to bad seeds with long runs of 0s or 1s that lead to relatively predictable results until the internal state is mixed up enough.</p> <p>However the constructor which takes a single value uses a complicated function on that seed value which is designed to minimize the likelihood of producing such 'bad' states. There's a second way to initialize <code>mt19937</code> where you directly set the internal state, via an object conforming to the SeedSequence concept. It's this second method of initialization where you may need to be concerned about choosing a 'good' state or doing warmup.</p> <hr> <p>The standard includes an object conforming to the SeedSequence concept, called <code>seed_seq</code>. <code>seed_seq</code> takes an arbitrary number of input seed values, and then performs certain operations on these values in order to produce a sequence of different values suitable for directly setting the internal state of a pRNG.</p> <p>Here's an example of loading up a seed sequence with enough random data to fill the entire <code>std::mt19937</code> state:</p> <pre><code>std::array&lt;int, 624&gt; seed_data; std::random_device r; std::generate_n(seed_data.data(), seed_data.size(), std::ref(r)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); std::mt19937 eng(seq); </code></pre> <p>This ensures that the entire state is randomized. Also, each engine specifies how much data it reads from the seed_sequence so you may want to read the docs to find that info for whatever engine you use.</p> <p>Although here I load up the seed_seq entirely from <code>std::random_device</code>, <code>seed_seq</code> is specified such that just a few numbers that aren't particularly random should work well. For example:</p> <pre><code>std::seed_seq seq{1, 2, 3, 4, 5}; std::mt19937 eng(seq); </code></pre> <p>In the comments below Cubbi indicates that <code>seed_seq</code> works by performing a warmup sequence for you.</p> <p>Here's what should be your 'default' for seeding:</p> <pre><code>std::random_device r; std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()}; std::mt19937 rng(seed); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload