Created
February 16, 2014 21:07
-
-
Save vo/9040649 to your computer and use it in GitHub Desktop.
Sample n samples without replacement from a population [0,N]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void sample(int * samples, size_t n, size_t N) { | |
size_t t = 0, m = 0; | |
while(m < n) | |
{ | |
double u = rand() / (double)RAND_MAX; | |
if((N-t)*u >= n-m) t++; | |
else samples[m++] = t++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment