简体   繁体   English

random_device vs default_random_engine

[英]random_device vs default_random_engine

#include <vector>
#include <random>

using namespace std;

int main()
{
    vector<int> coll{1, 2, 3, 4};
    shuffle(coll.begin(), coll.end(), random_device{});

    default_random_engine dre{random_device{}()};
    shuffle(coll.begin(), coll.end(), dre);
}

Question 1: What's the difference between 问题1:有什么区别

shuffle(coll.begin(), coll.end(), random_device{});

and

shuffle(coll.begin(), coll.end(), dre); ?

Question 2: Which is better? 问题2:哪个更好?

Question 1: What's the difference between... 问题1:......之间有什么区别?

std::random_device conceptually produces true random numbers. std::random_device概念上产生真正的随机数。 Some implementations will stall if you exhaust the system's source of entropy so this version may not perform as well. 如果您耗尽系统的熵源,某些实现将停止,因此此版本可能无法执行。

std::default_random_engine is a pseudo-random engine. std::default_random_engine是一个伪随机引擎。 Once seeded, with an random number it would be extremely difficult (but not impossible) to predict the next number. 一旦播种,随机数,预测下一个数字将非常困难(但并非不可能)。

There is another subtle difference. 还有另一个微妙的区别。 std::random_device::operator() will throw an exception if it fails to come up with a random number. std::random_device::operator()无法提供随机数,则会抛出异常。

Question 2: Which is better? 问题2:哪个更好?

It depends. 这取决于。 For most cases, you probably want the performance and temporal-determinism of the pseudorandom engine seeded with a random number, so that would be the second option. 对于大多数情况,您可能希望伪随机引擎的性能和时间确定性以随机数播种,因此这将是第二种选择。

Both random_device and default_random_engine are implementation defined. random_devicedefault_random_engine都是实现定义的。 random_device should provide a nondeterministic source of randomness if available, but it may also be a prng in some implementations. random_device应该提供一个非确定性的随机性来源(如果可用),但在一些实现中它也可能是一个prng。 Use random_device if you want unpredictable random numbers (most machines nowadays have hardware entropy sources). 如果你想要不可预测的随机数,请使用random_device(现在大多数机器都有硬件熵源)。 If you want pseudo random numbers you'd probably use one of the specific algorithms, like the mersenne_twister_engine . 如果你想要伪随机数,你可能会使用一种特定的算法,比如mersenne_twister_engine

I guess default_random_engine is what you'd use if you don't care about the particulars of how you get your random numbers. 我想如果你不关心如何获得随机数的详细信息,就会使用default_random_engine I'd suspect it'd just use rand under the hood or a linear_congruential_engine most of the time. 我怀疑它只是在引擎盖下使用rand或者大部分时间都使用linear_congruential_engine

I don't think the question "which is better" can be answered objectively. 我不认为“哪个更好”的问题可以客观地回答。 It depends on what you're trying to do with your random numbers. 这取决于你试图用你的随机数做什么。 If they're supposed to be random sources for some cryptographic process, I suspect default_random_engine is a terrible choice, although I'm not a security expert, so I'm not sure if even random_device is good enough. 如果它们应该是某些加密过程的随机源,我怀疑default_random_engine是一个糟糕的选择,虽然我不是安全专家,所以我不确定即使random_device也足够好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM