Skip to content

Instantly share code, notes, and snippets.

@vo
Created February 14, 2014 02:36
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/8994858 to your computer and use it in GitHub Desktop.
Save vo/8994858 to your computer and use it in GitHub Desktop.
Shuffle an array of ints
#include <stdlib.h>
void shuffle(int *array, size_t n)
{
if (n > 1) {
size_t i;
for (i = 0; i < n - 1; i++) {
size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
int t = array[j];
array[j] = array[i];
array[i] = t;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment