What is performance-wise the best way to generate random bools?

前端 未结 9 2181
滥情空心
滥情空心 2020-12-23 21:14

I need to generate random Boolean values on a performance-critical path.

The code which I wrote for this is

std::random_device   rd;
std::uniform_int         


        
9条回答
  •  遥遥无期
    2020-12-23 21:55

    Unless you have further constraints on the randomness you need, the fastest way to generate a random bool is:

    bool RandomBool() { return false; }
    

    To be more specific, there are thousands of ways to generate random boolean numbers, all satisfying different constraints, and many of them do not deliver "truly" random numbers (that includes all the other answers so far). The word "random" alone does not tell anyone what properties you really need.

提交回复
热议问题