**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(n–i);

10 index=i+index;

11 B[i]=A[index];

12 A[index]=A[i];

13 }

14 }

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(n–i);

10 index=i+index;

11 B[i]=A[index];

12 A[index]=A[i];

13 }

14 }

Advertisements