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?
for (auto &v:x) v=distribution_pos(generator);
- Sam Varshavchikstd::generate
. - davidbak<random>
library, they must have at least C++11, which means lambdas. - Brian Bistd::generate
will work with any generating function, not just the OP's specific example or<random>
, in case anyone's interested ... - davidbak