Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choose m elements randomly from a vector containing n elements

Tags:

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 kth 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.

like image 541
Vinay Avatar asked Feb 18 '12 23:02

Vinay


People also ask

How to randomly choose n items from list?

Select randomly n elements from a list using choice() The choice() method is used to return a random number from given sequence. The sequence can be a list or a tuple. This returns a single value from available data that considers duplicate values in the sequence(list).

How do I randomly select a vector in R?

There could be many ways to randomly select elements from an R vector. You can use the sample() or the runif() function to select elements from the vector.

How to select n number of elements from a list in Python?

Using Len() function to Get the Number of Elements We can use the len( ) function to return the number of elements present in the list.


1 Answers

You want a Fisher-Yates shuffle (stop after M iterations):

template<class BidiIter > BidiIter random_unique(BidiIter begin, BidiIter end, size_t num_random) {     size_t left = std::distance(begin, end);     while (num_random--) {         BidiIter r = begin;         std::advance(r, rand()%left);         std::swap(*begin, *r);         ++begin;         --left;     }     return begin; } 

Demo at http://ideone.com/3A3cv. This is significantly faster than std::random_shuffle when you only need a few random numbers out of the set, and should be just about the same speed even if N==M.

like image 189
Michael Burr Avatar answered Oct 03 '22 11:10

Michael Burr