Random Number Generator is always generating the same numbers

后端 未结 2 1082
误落风尘
误落风尘 2021-01-28 19:43

I am trying to fill my array with random numbers using the following piece of code

#include
#include

int main(){
int n = 5;
int li         


        
相关标签:
2条回答
  • 2021-01-28 20:22

    If a random hardware device is not available to the implementation, a pseudo random number engine is used. Is a random number hardware device available? By that, I mean not only physically there, but also available to that particular implementation of std::random_device.

    0 讨论(0)
  • 2021-01-28 20:23

    Call the entropy() member function on std::random_device to find out whether your implementation implements it properly:

    std::random_device may be implemented in terms of an implementation-defined pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation. In this case each std::random_device object may generate the same number sequence.

    (Source)

    If this is the case, a call to entropy() will return 0:

    A deterministic random number generator (e.g. a pseudo-random engine) has entropy zero.

    If that is the case, you need to use a different fallback mechanism for seeding. For instance, you could use a time-based seed like in the old C-days.

    On desktop platforms in particular, you should expect std::random_device to be implemented as a proper non-deterministic source though. If this is not the case, you might just be using a very old version of the standard library implementation. If you have the feeling that the implementation should support non-deterministic std::random_device but it does not, consider filing a bug report with your standard library maintainer.

    0 讨论(0)
提交回复
热议问题