Uncorrelated parallel random seeds since C ++ 2011?

I currently have a main application in Fortran that needs a seed to generate pseudo random numbers. I would like to run many (many) times this application with completely uncorrelated seeds (and, in addition, completely independent chains of pseudorandom numbers).

My question is: how to generate seeds with C ++ 2011?

+5
source share
3 answers

In your main thread, extract one seed (or sequence of seeds) from a good random source (e.g. from /dev/urandomLinux). Use this data to align one PRNG root. Then use this PRNG to generate seed values ​​for your local stream PRNGs.

#include <random>
#include <vector>

typedef std::mt19937 rng_type;
std::uniform_int_distribution<rng_type::result_type> udist;

int main()
{
    rng_type rng;

    // seed rng first, and store the result in a log file:
    rng_type::result_type const root_seed = get_seed();
    rng.seed(root_seed);

    // make thread seeds:
    std::vector<rng_type::result_type> seeds(NUMBER_OF_THREADS);
    for (auto & n : seeds) { n = udist(rng); }

    // make threads...
}

<random> , . , mt19937 .

+6

++ 11 std::random_device , . , , . lib++ /dev/urandom . libstd++ , _GLIBCXX_USE_RANDOM_TR1 . Visual Studio, , . edit: VS2012 Windows.

std::random_device (/dev/urandom PRNG), .

#include <random>

int main() {
    std::random_device r;
    std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
    std::mt19937 engine(seed);

}

, , . , . , , , .

std::random_device r;
std::vector<std::mt19937> engines;

int engines = 50;
for (int i = 0; i < engines; ++i) {
    std::seed_seq s{r(), r(), r(), r(), r(), r(), r(), r()};
    engines.emplace_back(s);
}

32- , 256 , , , . , .

, mt19937 mt19937::state_size (624) 32- . , , , , .

std::random_device r;
std::vector<std::uint_least32_t> data;
std::generate_n(back_inserter(data), 624, std::ref(r));

std::seed_seq seed(begin(data), end(data));

std::mt19937 engine(seed); // 'fully' seeded mt19937
+6

. -. , , (, /dev/urandom Linux), .

, , - , , , , .

++ 11 " ", . . -, , , .

0

All Articles