如何使C ++ Mersenne Twister 19937生成器在Windows和Linux上给出相同的结果? [英] How to get C++ Mersenne Twister 19937 generator to give the same results on Windows and Linux?

查看:29
本文介绍了如何使C ++ Mersenne Twister 19937生成器在Windows和Linux上给出相同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有以下代码来生成正态分布的随机数.

We have the following code to generate normally distributed random numbers.

   double average = 10.0; double spread = 4.0;
   std::mt19937 generator;
   generator.seed(1);
   std::normal_distribution<double> distribution(average, spread);

   for (uint i = 0; i < 10; i++)
      cout << "randomNumber=" << distribution(generator) << endl;

这应该在Linux和Windows上产生相同的确切序列.但事实并非如此.在同一操作系统上重新运行时,序列是相同的.但是在Linux和Windows上运行时,顺序是不同的.生成器是相同的,种子是相同的,因此它们应该给出相同的序列.

This should produce the same exact sequence on Linux and Windows. But it doesn't. The sequences are identical when rerun on the same OS. But the sequences are different when run on Linux vs. Windows. The generators are the same and the seeds are the same, so they should give identical sequences.

哪些遗漏或不正确的内容使代码可以在两个操作系统上创建相同的序列?

What's missing or incorrect that would allow the code to create identical sequences on both operating systems?

Windows10.VisualStudio 2010.

Windows 10. Visual Studio 2010.

Ubuntu 14.04.g ++(gcc 4.9.2)

Ubuntu 14.04. g++ (gcc 4.9.2)

Windows输出:

Windows Output:

randomNumber=10.6243
randomNumber=11.2256
randomNumber=7.72784
randomNumber=8.30245
randomNumber=6.77485
randomNumber=9.18181
randomNumber=5.19982
randomNumber=8.28505
randomNumber=5.24899
randomNumber=15.2219

Linux输出:

randomNumber=7.80102
randomNumber=4.38851
randomNumber=16.331
randomNumber=5.81941
randomNumber=11.0304
randomNumber=2.16242
randomNumber=3.96877
randomNumber=8.73883
randomNumber=13.4327
randomNumber=10.2854

推荐答案

C ++库规范保证 generator 将产生相同的值序列,但 distributions 不需要.

The C++ library specification guarantees that the generator will produce the same sequence of values, but the distributions are not required to.

如果您修改代码以从mt19937生成器生成原始输出,则会发现结果在各个平台上都是相同的:

If you modify your code to produce raw output from the mt19937 generator you will find that the results are the same across platforms:

std::mt19937 generator(1);

for (uint i = 0; i < 10; i++)
  cout << "randomNumber=" << generator() << endl;

如果分布在各个平台上都是确定性的,那肯定会很有用,但是我想委员会会认为这将使实施者放弃太多的自由来实施用于生成分布的新算法.

It certainly would be useful if the distributions were also deterministic across platforms, but I imagine the committee felt that would take away too much freedom from implementers to implement new algorithms for producing distributions.

无论如何,如果您想要确定性的分布,则需要自己实现它们或使用另一个提供确定性的跨平台实现的库.

Anyway, if you want deterministic distributions you'll need to implement them yourself or use another library that provides deterministic cross-platform implementations.

这篇关于如何使C ++ Mersenne Twister 19937生成器在Windows和Linux上给出相同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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