Random number generator from a set

Write a method to randomly generate a set of m integers from an array of size n. Each element must have equal probability of being chosen.

C++:
01 int randm(int m)
02 {
03     return rand()%m;
04 }
05 
06 void randomsubset(int *A, int n, int* B, int m)
07 {
08     for(int i=0;i<m;i++){
09         int index=randm(ni);
10         index=i+index;
11         B[i]=A[index];
12         A[index]=A[i];
13     }
14 }
Advertisements
This entry was posted in Probability. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s