How to use boost normal distribution classes?

Posted by David Alfonso on Stack Overflow See other posts from Stack Overflow or by David Alfonso
Published on 2010-01-16T18:33:58Z Indexed on 2011/02/17 23:25 UTC
Read the original article Hit count: 293

Hi all, I'm trying to use boost::normal_distribution in order to generate a normal distribution with mean 0 and sigma 1.

The following code uses boost normal classes. Am I using them correctly?

#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;
  }
}

The result on my machine is:

0.213436
-0.49558
1.57538
-1.0592
1.83927
1.88577
0.604675
-0.365983
-0.578264
-0.634376

As you can see all values are not between -1 and 1.

Thank you all in advance!

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost