Small example of sequence-based seeding of C++11 Mersenne Twister
text/x-c++src
466
Comments
Comment #0 by joseph.wakeling — 2017-01-07T22:22:41Z
Created attachment 1631
Small example of range-based seeding of Phobos Mersenne Twister
std.random's `MersenneTwisterEngine` includes a `seed` method that accepts an input range from which the state of the generator can be initialized. This method superficially seems similar to the sequence-based seeding mechanism that can be used when seeding the C++11 <random> `mt19937` engine, but its behaviour is different in several ways:
* the Phobos method will reject any range whose length is less than the size of the generator's internal state array. By contrast the C++11 `mt19937` accepts a seed sequence of any length (including 1);
* seeding with identical sequences produces completely different results for the C++11 and D versions. Besides simply being inconsistent (and probably also at odds with reference seeding methods for the Mersenne Twister) this may make it harder for users to port C++ code to D.
It is therefore suggested to implement a new range-based seeding mechanism that matches the C++11 seed-sequence behaviour, and deprecate the old seeding method.
Comment #1 by joseph.wakeling — 2017-01-07T22:24:16Z
Created attachment 1632
Small example of sequence-based seeding of C++11 Mersenne Twister
Compile with the `-std=c++11` option.
Comment #2 by joseph.wakeling — 2017-01-07T22:26:29Z
I've attached small code snippets to show the difference between the D and C++11 behaviour.
Running these on my machine the D code outputs (apart from the seed data) 1st and 10,000th variates of:
596004846
703773746
while the C++11 code outputs 1st and 10,000th variates of:
719125747
99701347
Comment #3 by robert.schadek — 2024-12-01T16:29:18Z