Created
February 14, 2014 02:36
-
-
Save vo/8994858 to your computer and use it in GitHub Desktop.
Shuffle an array of ints
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
#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