vendredi 14 septembre 2018

Create a vector with random numbers

How would I create a vector filled with random numbers?

The usual code one finds is along the lines of:

std::mt19937 rng {std::random_device{}()};
std::uniform_int_distribution<int> dist {1, 52};

std::vector<int> vec(10);
std::generate(begin(vec), end(vec), [&]{return dist(rng);} );

However this means that each value is touched twice: Once set to zero and then to the random value (even at O3)

So how to do this as efficient as possible?




Aucun commentaire:

Enregistrer un commentaire