Skip to content

Instantly share code, notes, and snippets.

@REPOmAN2v2
Created September 24, 2014 22:56
Show Gist options
  • Save REPOmAN2v2/2012b7384bd20a303fc4 to your computer and use it in GitHub Desktop.
Save REPOmAN2v2/2012b7384bd20a303fc4 to your computer and use it in GitHub Desktop.
Array shuffling in C
/* Fisher-Yates shuffle
Assume rand is already seeded */
void shuffle(my_type_t *A, size_t n)
{
int i, j;
my_type_t t;
for (i = n-1; i > 0; i--) {
j = rand() % (i+1) /* random int, 0 <= j <= i */
t = A[i];
A[i] = A[j];
A[j] = t;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment