如果我们在不同的机器上将c ++ 11 mt19937作为相同的种子,我们将得到相同的随机数序列 [英] If we seed c++11 mt19937 as the same on different machines, will we get the same sequence of random numbers

查看:329
本文介绍了如果我们在不同的机器上将c ++ 11 mt19937作为相同的种子,我们将得到相同的随机数序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和类似的问题启发,我想学习mt19937 C ++ 11中的伪数字生成器的行为是,在两台单独的计算机中,它使用相同的输入作为种子.

换句话说,我们有以下代码;

std::mt19937 gen{ourSeed};
std::uniform_int_distribution<int> dest{0, 10000};
int randNumber = dist(gen);

如果我们在不同的时间在不同的机器上尝试此代码,那么我们将获得相同的randNumber值序列还是每次获得不同的序列?

无论哪种情况,为什么会这样?

另一个问题:

无论种子如何,此代码都将无限生成随机数吗?我的意思是,例如,如果我们在运行数月而不停止的程序中使用此代码块,数字的生成或数字的统一性是否会出现任何问题?

解决方案

生成器将生成相同的值.

发行版可能不会,至少对于不同的编译器或库版本而言.该标准没有详细说明其行为.如果要在编译器和库版本之间保持稳定性,则必须滚动自己的发行版.

禁止库/编译器更改,这些更改将以相同的顺序返回相同的值.但是,如果您愿意的话,请编写自己的发行版.

...

所有PRNG都有模式和句点. mt19937 以其2 ^ 19937-1时期命名,这不太可能是问题.但是其他模式也会发展. MT PRNG可以抵抗许多统计测试,但是它们并不是冷冻安全的PRNG.

因此,如果您运行数月,就会遇到问题,这将取决于发现问题的具体细节.但是,mt19937将成为比您可能会自己写的任何东西更好的PRNG.但是,假设攻击者可以根据过去的证据预测其未来的行为.

Inspired from this and the similar questions, I want to learn how does mt19937 pseudo-number generator in C++11 behaves, when in two separate machines, it is seeded with the same input.

In other words, say we have the following code;

std::mt19937 gen{ourSeed};
std::uniform_int_distribution<int> dest{0, 10000};
int randNumber = dist(gen);

If we try this code on different machines at different times, will we get the same sequence of randNumber values or a different sequence each time ?

And in either case, why this is the case ?

A further question:

Regardless of the seed, will this code generate randomly numbers infinitely ? I mean for example, if we use this block of code in a program that runs for months without stopping, will there be any problem in the generation of the number or in the uniformity of the numbers ?

解决方案

The generator will generate the same values.

The distributions may not, at least with different compilers or library versions. The standard did not specify their behaviour to that level of detail. If you want stability between compilers and library versions, you have to roll your own distribution.

Barring library/compiler changes, that will return the same values in the same sequence. But if you care write your own distribution.

...

All PRNGs have patterns and periods. mt19937 is named after its period of 2^19937-1, which is unlikely to be a problem. But other patterns can develop. MT PRNGs are robust against many statistical tests, but they are not crytographically secure PRNGs.

So it being a problem if you run for months will depend on specific details of what you'd find to be a problem. However, mt19937 is going to be a better PRNG than anything you are likely to write yourself. But assume attackers can predict its future behaviour from past evidence.

这篇关于如果我们在不同的机器上将c ++ 11 mt19937作为相同的种子,我们将得到相同的随机数序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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