This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: where are the implementations for <random>?
On Tue, Apr 11, 2017 at 09:27:53PM +0100, Jonathan Wakely wrote:
> > All what there is seems to be
> > http://www.cplusplus.com/reference/random/mersenne_twister_engine/
>
> Use a better reference. http://en.cppreference.com is much better than
> cplusplus.com
>
Actually I also noted that some time ago (the above was just what came
out first from the search engine (unfortunately)).
> I don't understand what you're trying to do or what the difficulty is.
>
> The output of the mersenne_twister_engine is specified by the
> standard, there should be no need to reimplement it. If you don't get
> repeatable values for any implementation of mersenne_twister_engine
> then it's a bug in that implementation.
>
> The template arguments are just parameters for the internal algorithm,
> so different parameters create different outputs. std::mt19937 is
> simply a specialization of the mersenne_twister_engine with parameters
> that are known to produce good pseudo-random numbers.
>
> The behaviour of uniform_int_distribution is also specified by the
> standard, but only the mathematical properties, not a specific
> implementation. That means different implementations can give
> different outputs. If you use your own uniform_int_distribution then
> you ensure repeatable outputs across implementations. I repeat, there
> should be no need to do that with the engines themselves.
Consider the following program MT.cpp:
#include <iostream>
#include <random>
#include <vector>
#include <cstdint>
int main() {
typedef std::mt19937_64::result_type ui;
std::seed_seq s {ui(10),ui(10),ui(0)};
std::vector<std::uint32_t> seeds(3);
s.generate(seeds.begin(), seeds.end());
for (const auto x : seeds) std::cout << x << " ";
std::cout << "\n";
std::mt19937_64 g(s);
std::cout << g() << " " << g() << "\n";
}
Compiled with
> g++ --std=c++11 -Wall MT.cpp
it yields with gcc 4.7.4 and gcc 5.4
> ./a.out
927367489 2598207009 681269367
14538134740155241067 15440504459514664889
As far as I understand it, only the first line is defined by the standard,
but not the second: how the seed-sequence is used by the generator g is up
to the compiler.
Thus the need to do the seeding ourselves.
Oliver