相关疑难解决方法(0)

在C/C++中按照正态分布生成随机数

如何在C或C++中正常分布后轻松生成随机数?

我不想使用Boost.

我知道Knuth详细谈论了这个问题,但我现在还没有他的书.

c c++ random distribution normal-distribution

112
推荐指数
8
解决办法
18万
查看次数

如何使用boost正态分布类?

我正在尝试使用boost :: normal_distribution来生成具有均值0和sigma 1的正态分布.

以下代码不起作用,因为某些值超过或超过-1和1(并且不应该).有人能指出我做错了什么吗?

#include <boost/random.hpp>
#include <boost/random/normal_distribution.hpp>

int main()
{
  boost::mt19937 rng; // I don't seed it on purpouse (it's not relevant)

  boost::normal_distribution<> nd(0.0, 1.0);

  boost::variate_generator<boost::mt19937&, 
                           boost::normal_distribution<> > var_nor(rng, nd);

  int i = 0; for (; i < 10; ++i)
  {
    double d = var_nor();
    std::cout << d << std::endl;
  }
}
Run Code Online (Sandbox Code Playgroud)

我的机器上的结果是:

0.213436
-0.49558
1.57538
-1.0592
1.83927
1.88577
0.604675
-0.365983
-0.578264
-0.634376
Run Code Online (Sandbox Code Playgroud)

如您所见,所有值都不在-1和1之间.

谢谢大家!

编辑:当你有最后期限并且在做练习之前避免研究理论时会发生这种情况.

c++ statistics boost normal-distribution

24
推荐指数
2
解决办法
3万
查看次数

标签 统计

c++ ×2

normal-distribution ×2

boost ×1

c ×1

distribution ×1

random ×1

statistics ×1