根据标准,std::random_device
按以下方式工作:
result_type operator()();
Returns: A non-deterministic random value, uniformly distributed between
min()
andmax()
, inclusive. It is implementation-defined how these values are generated.
您可以通过多种方式使用它。为引擎播种:
std::mt19937 eng(std::random_device{}());
本身就是一个引擎:
std::uniform_int_distribution<> uid(1, 10);
std::cout << dist(dev);
因为它是实现定义的,所以它听起来不如 std::seed_seq
或 srand(time(nullptr))
强。我更喜欢将它用作种子、引擎还是根本不用?
最佳答案
一般来说,std::random_device
应该是您可以在您的平台上访问的最真实随机信息的来源。话虽这么说,访问它比 std::mt19937
或其他什么慢得多。
正确的行为是使用 std::random_device
来播种类似 std::mt19937
的内容。
关于c++ - 什么时候使用 std::random_device?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27492762/