Should I call reset() on my C++ std random distribution to clear hidden state?

后端 未结 3 766
天涯浪人
天涯浪人 2020-12-11 16:01

I would like to wrap the random number distributions from the C++11 standard library with simple functions that take as arguments the distribution\'s parameters and a genera

3条回答
  •  粉色の甜心
    2020-12-11 16:38

    To ensure there is no hidden state within the distribution, is it 1) necessary

    Yes.

    and 2) sufficient to call reset() on the distribution each time?

    Yes.

    You probably don't want to do this though. At least not on every single call. The std::normal_distribution is the poster-child for allowing distributions to maintain state. For example a popular implementation will use the Box-Muller transformation to compute two random numbers at once, but hand you back only one of them, saving the other for the next time you call. Calling reset() prior to the next call would cause the distribution to throw away this already valid result, and cut the efficiency of the algorithm in half.

提交回复
热议问题