lundi 1 janvier 2018

How to set parameters of a uniform distribution in C++?

There are k different uniform distributions of integer type. There ranges are not necessarily the same, say dist1~U(0,10), dist2~U(0,5), etc. I'm using the uniform_int_distribution class in C++14. Here is my code:

#include <vector>
#include <random>

using namespace std;

int main() {
  vector<int> distRange = {4,5,2,4};
  vector< uniform_int_distribution<int> > U(distRange.size(),uniform_int_distribution<int>(0,1));

  for (int i=0; i<distRange.size(); i++)
    U[i].param( uniform_int_distribution<int>(0,distRange[i]).param() );

  return 0;
}

As we can see, I created a new object to change the parameters. Is there a cheaper way (number of typing, or less running time) to do that?

Aucun commentaire:

Enregistrer un commentaire