问题描述
受this和类似问题的启发,我想了解 mt19937C++11 中的伪数生成器的行为,当在两台不同的机器上时,它以相同的输入作为种子.
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);
如果我们在不同的时间在不同的机器上尝试这段代码,我们会得到相同序列的 randNumber 值还是每次不同的序列?
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 ?
另一个问题:
不管种子如何,这段代码会无限生成随机数吗?我的意思是比如说,如果我们在一个连续运行几个月的程序中使用这块代码,会不会出现数字的生成问题或数字的统一性问题?
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 ?
推荐答案
generator 将生成相同的值.
发行版 可能不会,至少在不同的编译器或库版本中是这样.该标准没有详细说明他们的行为.如果您想要编译器和库版本之间的稳定性,则必须推出自己的发行版.
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.
...
所有 PRNG 都有模式和句点.mt19937 以其周期 2^19937-1 命名,这不太可能是问题.但其他模式可以发展.MT PRNG 对许多统计测试都很稳健,但它们在密码学上不是安全的 PRNG.
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.
因此,如果您运行数月,这将是一个问题,这取决于您发现问题的具体细节.但是,mt19937 将是比您可能自己编写的任何内容都更好的 PRNG.但假设攻击者可以根据过去的证据预测其未来的行为.
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 相同,我们会得到相同的随机数序列吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!