Skip to content

Instantly share code, notes, and snippets.

@vo
Created February 16, 2014 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vo/9040649 to your computer and use it in GitHub Desktop.
Save vo/9040649 to your computer and use it in GitHub Desktop.
Sample n samples without replacement from a population [0,N]
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