Why not just use random_device?

前端 未结 2 594
半阙折子戏
半阙折子戏 2020-12-04 13:33

I am a bit confused about the c++11 random library.

What I understand: we need two separate concepts:

  • random engine (which can be pseudo (need seed) or
2条回答
  •  离开以前
    2020-12-04 14:14

    This article is a good point to start.

    I'm going to synthesize just few points:

    • It Has Unknown Cost.

      How costly it is to read a number from this “device”? That is unspecified. It could, for example, be reading from /dev/random on a Linux system, which can block for an extended period waiting for entropy (which is itself problematic for a variety of reasons).

    For my personal experience I've notified that std::random_device is usually slower than a simple Pseudo-randomic algorithm. That could be no true in general, but usually it does. That because it may involve physical devices, or other hardware than the simple CPU.

    • It Might Actually Be Deterministic .

      C++11's std::random_device is not required to be nondeterministic! Implementations can and do implement it as a simple RNG with a fixed seed, so it produces the same output for every run of the program.

提交回复
热议问题