将std :: random_device与pRNG例如一起使用有什么区别std :: mt19937和没有? [英] What is the difference between using std::random_device with pRNG e.g. std::mt19937 and without?
问题描述
在C ++ 11中,可以使用std::random_device
生成数字,可以使用或不使用诸如mt19937之类的伪随机数生成器.
In C++11 one can generate numbers with the use of std::random_device
with or without a pseudo random number generator like mt19937.
在此示例代码中使用此代码有什么区别?
What will be the difference using this in this exemplar code:
#include <random>
#include <iostream>
int main() {
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<double> dist(1, 10);
for (int i=0; i<16; ++i)
std::cout << dist(rd) << "\t" << dist(mt) << "\n";
}
推荐答案
std::random_device
应该为您提供诸如mt19937
之类的引擎的种子.产生的连续数字的质量是完全不确定的,对于实际目的(例如密码学)可能很容易不足,因此依靠它是毫无疑问的.
std::random_device
is supposed to get you a seed for engines like mt19937
. The quality of successive numbers produced is completely undefined and may easily be insufficient for practical purposes (such as cryptography), so relying on that is out of question.
除此之外,mt19937
将在给定相同种子的情况下为您提供相同的序列. random_device
s的值只能受到为其构造函数提供的字符串的影响……这意味着实现定义的行为.
Apart from that, mt19937
will give you the same sequence when given the same seed. A random_device
s values can be only influenced by the string given to its constructor... which implies implementation-defined behavior.
这篇关于将std :: random_device与pRNG例如一起使用有什么区别std :: mt19937和没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!