gpt4 book ai didi

c++ - 什么时候使用 std::random_device?

转载 作者:可可西里 更新时间:2023-11-01 15:09:00 26 4
gpt4 key购买 nike

根据标准,std::random_device 按以下方式工作:

result_type operator()();

Returns: A non-deterministic random value, uniformly distributed between min() and max(), 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_seqsrand(time(nullptr)) 强。我更喜欢将它用作种子、引擎还是根本不用?

最佳答案

一般来说,std::random_device 应该是您可以在您的平台上访问的最真实随机信息的来源。话虽这么说,访问它比 std::mt19937 或其他什么慢得多。

正确的行为是使用 std::random_device 来播种类似 std::mt19937 的内容。

关于c++ - 什么时候使用 std::random_device?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27492762/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com