#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
// Define the size of the array and initialize it.
#define ARRAY_SIZE 10
int array[ARRAY_SIZE];
// For this example the array to be shuffled is filled with the values 0..9.
int i;
for (i = 0; i < ARRAY_SIZE; i++)
array[i] = i;
// Initialize the random number generator.
srand((unsigned) time(NULL));
// Start at the length of the array.
int index = ARRAY_SIZE;
// Note the use of "index > 1" rather than "index > 0". If "index > 0" was used
// then the final pass through the loop would always swap index 0 with itself.
while (index > 1)
{
// Create a random number in the range 0..index-1.
int randomNum = (int) ((double) rand() / ((double) RAND_MAX + 1) * index);
// Decrement index by 1, the loop travels down through the array.
index = index - 1;
// Swap the array elements at positions index and randomNum.
int temp = array[index];
array[index] = array[randomNum];
array[randomNum] = temp;
}
return 0;
}