vendredi 21 septembre 2018

pass C++ random number distribution to a function

I have looked for similar questions but haven't found them. I want to generate normally distributed random numbers. I used to code C and some C++98 but am now trying to go back and learn C++11.

I have a function to return a seeded RNG

auto seeded_rng () {
      ....  //do seeding.
     std::default_random_engine Eng(/*seeds*/);
     return Eng;
}

In my main function I bind the RNG to say a gaussian distribution

auto binded = std::bind(std::normal_distribution{0,1.0},seeded_rng);

This function works fine. I can call "binded()" directly in main and it generates the numbers

I want to have a simulation object that needs random numbers to be created. My question related to how to pass in the "RNG_PART" below.

class sim
{
public:
       sim( RNG_PART & rng, int_number_of sims ){ /* Do whatever */}
}

So if in main, I then want to create a simulation object

sim A(binded, 100);

it complains. I tried declaring

sim::sim(std::default_random_engine &rng, int number_of_sims){}

but it is complaining. What type should I use to pass in the "binded" distribution to the constructor? Or am I going about this completely incorrectly. Should I just declare the RNG engine globally? I'd prefer not to do that.

Apologies if this is very basic!




Aucun commentaire:

Enregistrer un commentaire