C++ TR1: how to use the normal_distribution?

后端 未结 4 1675
野的像风
野的像风 2020-12-11 03:42

I\'m trying to use the C++ STD TechnicalReport1 extensions to generate numbers following a normal distribution, but this code (adapted from this article):

4条回答
  •  囚心锁ツ
    2020-12-11 04:11

    If your TR1 random number generation implementation is buggy, you can avoid TR1 by writing your own normal generator as follows.

    Generate two uniform (0, 1) random samples u and v using any random generator you trust. Then let r = sqrt( -2 log(u) ) and return x = r sin(2 pi v). (This is called the Box-Mueller method.)

    If you need normal samples samples with mean mu and standard deviation sigma, return sigma*x + mu instead of just x.

提交回复
热议问题