Boost logo

Boost Users :

Subject: Re: [Boost-users] [Random] Getting normal_distribution<> to work
From: Nathan Crookston (nathan.crookston_at_[hidden])
Date: 2010-07-28 12:52:33


Hi Hossein,

On Wed, Jul 28, 2010 at 10:33 AM, Hossein Haeri <powerprogman_at_[hidden]> wrote:
> Dear all,
>
> When I run the following simple code, the output is "-1.#IND" in MSVC 2008 and "nan" in GCC 4.4.0. I guess that means something is not initialised yet... So, what's missing here?
>
> boost::normal_distribution<double> gen;
> boost::mt19937 engine;
> double r = gen(engine);
> cout << r;

Looks like you're forgetting to pass both arguments to the
variate_generator -- the item you marked as gen is really a
distribution.

Try the following:

#include "boost/random/mersenne_twister.hpp"
#include "boost/random/normal_distribution.hpp"
#include "boost/random/variate_generator.hpp"

#include <iostream>

int main()
{
  boost::normal_distribution<double> dist;
  boost::mt19937 engine;
  boost::variate_generator<boost::mt19937&,
     boost::normal_distribution<double> > gen(engine, dist);
  double r = gen();
  std::cout << r;
  return 0;
}

HTH,
Nate


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net