如何通过< random>在多种类型的编译器和内核上生成相同的随机数序列? [英] How to generate the same random number sequence over multiple types of compilers and kernels with <random>?

查看:87
本文介绍了如何通过< random>在多种类型的编译器和内核上生成相同的随机数序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在不同的机器和编译器上产生相同的(伪)随机数序列.如果我使用相同的内核,则似乎在g ++中实现了mersennetwist(MT):不管我是否在使用g ++ 4.9或4.7的较新机器上编译程序,我都会得到相同的随机数.但是,如果使用较旧的内核或更改为Visual Studio的编译器,则会得到不同的结果.可以,因为没有保证mersenne_twister_engine::seed在不同的编译器上将内部状态设置为相同.

I need to produce the same (pseudo) random number sequence on different machines and compilers. If I use the same kernel, it seems that the implementetion of mersenne twister (MT) in g++ works well: regardless if I compile my program on a newer machine, with g++ 4.9 or 4.7, I get the same random numbers. But I get different ones if I use older kernel or if I change to Visual Studio's compiler. That's ok, because there's no gurantee that mersenne_twister_engine::seed sets the internal state to the same over different compilers.

我坚信,在生成器上应用operator<<会产生一个独特的结果,该结果可用于在带有operator>>的其他计算机上设置生成器,但是对于mt19937来说,这似乎是行不通的.为了清楚起见,在计算机A上,我有代码

I tought that applying operator<< on the generator produces a unique result that can be used to set the generators on other machines with the operator>>, but in case of mt19937, it seems it is not working. To make it clear, on a computer A I had the code

mt19937 generator1A;
uniform_int_distribution<int> distribution(0, 1000);

cout << "Generating random numbers with seed 1000" << endl;

generator1A.seed(1000);
generator1A(); //to advance the state by one so operator>> will give a longer output; this is not necessary indeed
ofstream test("testseed1000.txt");
test << generator1A << endl;

for (int i = 0; i < 10; ++i)
    cout << distribution(generator1A) << endl;

它会生成252、590、893,...和一个长文件.我将文件传输到另一台计算机B,然后运行以下代码:

And it produces 252, 590, 893, ..., and a long file. I transfer the file to the other machine B, and run the following code:

mt19937 generator1B, generator2B;
uniform_int_distribution<int> distribution(0, 1000);

cout << "Generating random numbers with seed 1000, and with operator>>" << endl;
generator2B.seed(1000);
generator2B(); // to advance the state by one here as well

ifstream test("testseed1000.txt");

test >> generator1B;
cout << "************************" << endl;
cout << generator1B << endl;
cout << "************************" << endl;
cout << "With seed\twith operator>>" << endl;

for (int i = 0; i < 10; ++i)
    cout << distribution(generator2B) << "\t" << distribution(generator1B) << endl;

它产生

654     205
205     115
115     610

问题

您能否提供建议,至少在Windows上使用VC ++,在Debian和Ubuntu上使用g ++,如何生成相同的(伪)随机数?如果可能的话,我想使用std,并且我不想实现自己的MT引擎.

The question

Can you give advices how to generate the same (pseudo) random numbers with at least VC++ on Windows and g++ on Debian and Ubuntu? I'd like to use std if it is possible and I wouldn't like to implement my own MT engine.

  • 创建数百万个随机数然后读入不是解决方案
  • 我必须使用MSVS进行代码开发,并使用unix服务器进行仿真
  • 除了MT引擎之外,我们也欢迎
  • 但我更喜欢MT
  • creating millions of random numbers and then reading in is not a solution
  • I have to use MSVS for code developing and unix servers for simulation
  • other than MT engines are also welcomed but I prefer MT

推荐答案

要从问题中删除变量,请尝试查看随机数引擎的输出,而不是将其输入分布.

To remove a variable from the problem, try looking at the outputs from your random number engine, rather than feeding it into a distribution.

我只是看了uniform_int_distribution的一些文档页面,并且没有给出有关如何使用统一随机数生成器的输出的任何声明.我不确定标准怎么说.

I just looked at a few documentation pages for uniform_int_distribution, and it does not give any statement about how the outputs from the uniform random number generator are used. I'm not sure what the standard says.

我预计您 从mersenne捻线机获得了相同的输出,但是您正在将这些输出馈送到uniform_int_distribution的两个不等价的实现中.

I predict that you are getting the same outputs from your mersenne twister, but you're feeding those outputs into two inequivalent implementations of uniform_int_distribution.

如果是这样,则要获得相同的随机数序列,您将必须使用分布函数的外部实现来确保在所有系统上获得相同的结果.

If this is true, then to get the same random number sequence, you're going to have to use an external implementation of the distribution functions to ensure that you get the same results on all systems.

这篇关于如何通过&lt; random&gt;在多种类型的编译器和内核上生成相同的随机数序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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