for (Dest = N-1; Dest > 0; Dest--)
{
   Src = rand() % (Dest+1);  // Positions from [0] to [Dest]
   Swap (X[Src], X[Dest]);
}

Example 2: C++ implementation of shuffling algorithm that does generate all permutations with equal probability.

Back to Article