where are the implementations for <random>?

Martin Sebor msebor@gmail.com
Sun Apr 9 23:32:00 GMT 2017

On 04/09/2017 12:23 PM, Marc Glisse wrote:
> On Sun, 9 Apr 2017, Oliver Kullmann wrote:
>> Hello,
>> I thought that with the facilities from <random> we had good and
>> well-defined ones -- but alas, only recently I realised that only say
>> the Mersenne twister is well-defined in the standard, but roughly
>> everything else is up to the implementer.
>> Since reproducibility is crucial for us (as with most scientific
>> applications), thus we need to implement ourselves the algorithms for
>> 1. std::seed_seq
>> 2. std::uniform_int_distribution
>> 3. std::bernoulli_distribution (only for default p=0.5).
>> Easiest would be to copy (and specialise) the gcc-implementations.

It's worth keeping in mind that copying libstdc++ code will have
licensing implications.  A different caveat is that conforming C++
programs can define explicit specializations of standard library
templates only for user-defined types.  I.e., it's not valid to
define one's own std::uniform_int_ditribution class template or
specialization of it on a fundamental type such as int.

>> I searched for it in the source-code, but couldn't find it:
>> Where do I find the implementations for <random> ?
> Option 1)
> First you find the file called "random" (/usr/include/c++/6/random
> here), then you notice that it contains
> #include <bits/random.h>
> #include <bits/opt_random.h>
> #include <bits/random.tcc>
> so you look for those files (and recursively if needed)
> Option 2)
> You compile a file that contains #include <random> using the -E flag,
> and look for bernoulli_distribution or whatever in the output.
> Option 3)
> Get the sources for gcc.
> grep -r bernoulli_distribution /the/sources

Another option is to browse the sources online:



> They all point to bits/random.h (with one function implemented in
> bits/random.tcc), with older versions in tr1/.

More information about the Gcc-help mailing list