I have a vector containing n
elements. I need to choose a subset of m
elements randomly from the vector without repetition. What is the most efficient way of doing this? I need to do this several thousands of times in my code.
The solution on top of my mind is to use rand()
to generate a random number k
between 0
and n
. Then pick the k
th element in the vector and insert it into a std::set
. Keep doing this till the set's size becomes equal to m
. I am now assured that the set contains m
unique elements randomly chosen from the set of n
elements.
What are the other possible solutions?
Thanks.
std::random_shuffle()
on vector and pull firstm
elements out of it, perhaps? – Bawbeem
is much smaller thann
. – Pursystd::random_shuffle()
is just a full permutation which also uses Fisher-Yates shuffle. See the first version of possible implementation from cppreference. For those who wants to understand the accepted answer and alsostd::random_shuffle()
, it's worth for a read. – Cloacam
elements, it does the entire data set, which is a massive waste of time. Its the difference betweenstd::sort
andstd::partial_sort
. – Pursy