Random shuffling of elements in STL containers

Use stl algorithm random_shuffle. This function has two overloads, first version randomizes using it’s own version of random number generator, second function takes a custom random number generator from our side.

[sourcecode language=”cpp”]void TestShuffle()
{
    typedef short VT;
    typedef std::vector VTVector;

    // Count of elements
    const VTVector::size_type Size = 10;

    // Create two vector objects
    VTVector Vect( Size );

    // Generate 10 random numbers
    std::generate( Vect.begin(), Vect.end(), rand );
    // Sort them
    std::sort( Vect.begin(), Vect.end() );
    // Shuffle them to demonstrate shuffle!!!
    std::random_shuffle( Vect.begin(), Vect.end() );
}

//Debugger output after generate call…
//10228,19974,5968,8993,14868,12724,4693,25377,32034,7329
//Debugger output after sort
//4693,5968,7329,8993,10228,12724,14868,19974,25377,32034
//Debugger output after random_shuffle call
//4693,14868,10228,19974,12724,7329,32034,8993,25377,5968[/sourcecode]

Appreciate your comments...