Created
September 24, 2014 22:56
-
-
Save REPOmAN2v2/2012b7384bd20a303fc4 to your computer and use it in GitHub Desktop.
Array shuffling in C
This file contains hidden or 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
/* 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