I would like to loop over several random combinations. Currently, I define a vector v
with the numbers 1
to n
outside the loop, shuffle v
inside the loop and define a new vector combination
inside the loop.
int k = 50;
int n = 100;
int sampleSize=100;
std::vector<int> v(n);
//std::vector<int> combination(k); //Would it be better to declare here?
std::iota(v.begin(), v.end(), 0);
unsigned seed = 42;
for (int i=0; i<sampleSize; i++) {
std::shuffle (v.begin(), v.end(), std::default_random_engine(seed));
std::vector<int> combination(v.begin(), v.begin() + k);
};
It seems weird to me that I define combination
again in every iteration of the for loop. Would it make sense to declare combination
outside of the for loop and then assign new values to it in every iteration? If so, what would be a good way to assign those new values to combination
? So far, I have only used push_back()
to append new values to a vector.