1

I have found this example on How to use boost normal distribution classes?

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

I was wondering what I have do add to the code to generate a different random numbers each time. I am using the number called produced d in a for loop, but at every time I run the program I always obtain the same numbers. Thank you!!!!

kiyah
  • 1,502
  • 2
  • 18
  • 27
J.Doe
  • 109
  • 1
  • 1
  • 8
  • Why don't you seed the engine before passing it to the constructor of `variate_generator`? – Galik Feb 04 '18 at 12:30
  • Hey friend , do you mean here : boost::mt19937 rng; ? I should pass an argument to rng ? It is what I would like to discover.. – J.Doe Feb 04 '18 at 12:34

2 Answers2

1

I solved in this way and it seems to work:

I added the library:

      #include<time.h>

and modifying just this:

    boost::mt19937 rng(time(0));

in this way I guess that the seed is the time of the computer.

J.Doe
  • 109
  • 1
  • 1
  • 8
0

You seed the engine. See e.g. This answer which includes many many distributions: How do I use Boost Random

sehe
  • 374,641
  • 47
  • 450
  • 633