問題描述
受this和類似問題的啟發(fā),我想了解 mt19937C++11 中的偽數(shù)生成器的行為,當在兩臺不同的機器上時,它以相同的輸入作為種子.
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.
換句話說,假設(shè)我們有以下代碼;
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 ?
另一個問題:
不管種子如何,這段代碼會無限生成隨機數(shù)嗎?我的意思是比如說,如果我們在一個連續(xù)運行幾個月的程序中使用這塊代碼,會不會出現(xiàn)數(shù)字的生成問題或數(shù)字的統(tǒng)一性問題?
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 將生成相同的值.
發(fā)行版 可能不會,至少在不同的編譯器或庫版本中是這樣.該標準沒有詳細說明他們的行為.如果您想要編譯器和庫版本之間的穩(wěn)定性,則必須推出自己的發(fā)行版.
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.
除非庫/編譯器更改,否則將以相同的順序返回相同的值.但是,如果您愿意編寫自己的發(fā)行版.
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 命名,這不太可能是問題.但其他模式可以發(fā)展.MT PRNG 對許多統(tǒng)計測試都很穩(wěn)健,但它們在密碼學(xué)上不是安全的 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.
因此,如果您運行數(shù)月,這將是一個問題,這取決于您發(fā)現(xiàn)問題的具體細節(jié).但是,mt19937 將是比您可能自己編寫的任何內(nèi)容都更好的 PRNG.但假設(shè)攻擊者可以根據(jù)過去的證據(jù)預(yù)測其未來的行為.
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.
這篇關(guān)于如果我們在不同的機器上播種 c++11 mt19937 相同,我們會得到相同的隨機數(shù)序列嗎的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!