問(wèn)題描述
受this和類(lèi)似問(wèn)題的啟發(fā),我想了解 mt19937C++11 中的偽數(shù)生成器的行為,當(dāng)在兩臺(tái)不同的機(jī)器上時(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.
換句話說(shuō),假設(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);
如果我們?cè)诓煌臅r(shí)間在不同的機(jī)器上嘗試這段代碼,我們會(huì)得到相同序列的 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 ?
無(wú)論哪種情況,為什么會(huì)這樣?
And in either case, why this is the case ?
另一個(gè)問(wèn)題:
不管種子如何,這段代碼會(huì)無(wú)限生成隨機(jī)數(shù)嗎?我的意思是比如說(shuō),如果我們?cè)谝粋€(gè)連續(xù)運(yùn)行幾個(gè)月的程序中使用這塊代碼,會(huì)不會(huì)出現(xiàn)數(shù)字的生成問(wèn)題或數(shù)字的統(tǒng)一性問(wèn)題?
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ā)行版 可能不會(huì),至少在不同的編譯器或庫(kù)版本中是這樣.該標(biāo)準(zhǔn)沒(méi)有詳細(xì)說(shuō)明他們的行為.如果您想要編譯器和庫(kù)版本之間的穩(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.
除非庫(kù)/編譯器更改,否則將以相同的順序返回相同的值.但是,如果您愿意編寫(xiě)自己的發(fā)行版.
Barring library/compiler changes, that will return the same values in the same sequence. But if you care write your own distribution.
...
所有 PRNG 都有模式和句點(diǎn).mt19937 以其周期 2^19937-1 命名,這不太可能是問(wèn)題.但其他模式可以發(fā)展.MT PRNG 對(duì)許多統(tǒng)計(jì)測(cè)試都很穩(wěn)健,但它們?cè)诿艽a學(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.
因此,如果您運(yùn)行數(shù)月,這將是一個(gè)問(wèn)題,這取決于您發(fā)現(xiàn)問(wèn)題的具體細(xì)節(jié).但是,mt19937 將是比您可能自己編寫(xiě)的任何內(nèi)容都更好的 PRNG.但假設(shè)攻擊者可以根據(jù)過(guò)去的證據(jù)預(yù)測(cè)其未來(lái)的行為.
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è)诓煌臋C(jī)器上播種 c++11 mt19937 相同,我們會(huì)得到相同的隨機(jī)數(shù)序列嗎的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!