不相关的并行随机种子与C ++ 2011? [英] Uncorrelated parallel random seeds with C++ 2011?

查看:64
本文介绍了不相关的并行随机种子与C ++ 2011?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我在Fortran中有一个主要应用程序,需要一个种子来生成伪随机数. 我想用完全不相关的种子(以及完全独立的伪随机数链)运行此应用程序很多次(很多次).

Currently, I have a main application in Fortran that need a seed to generate pseudo-random numbers. I would like to run many (many) times this application with completely uncorrelated seeds (and furthermore completely independent pseudo-random numbers chains).

我的问题是:如何使用C ++ 2011生成种子?

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

推荐答案

在主线程中,从良好的随机源(例如,从Linux上的/dev/urandom)中提取单个种子(或种子序列).使用该数据来播种单个根PRNG.然后使用 that PRNG为您的线程本地PRNG生成种子值.

In your main thread, extract a single seed (or seed sequence) from a good random source (e.g. from /dev/urandom on Linux). Use that data to seed a single root PRNG. Then use that PRNG to generate seed values for your thread-local 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.

The random number engine interface in <random> lets you seed both from a single integer and from a sequence of integers. If you want additional randomness, you can seed the mt19937 from a sequence of several hundred integers.

这篇关于不相关的并行随机种子与C ++ 2011?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆