lundi 17 décembre 2018

Initialize an N-sized std::vector with std::uniform_real_distribution

I have a std::vector<double> x, who's size I do not know at compile time. Assuming the size of the vector is N, I want to assign N uniformly distributed random values to it. I currently do this within a loop

std::default_random_engine generator;
std::uniform_real_distribution<double> distribution_pos(0.0,1.0);
for (auto it = x.begin(); it != x.end(); it++)
{
  *it = distribution_pos(generator);
}

This doesn't seem very elegant to me, so I was wondering if there was a smarter way to do this?




Aucun commentaire:

Enregistrer un commentaire