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…
//Debugger output after sort
//Debugger output after random_shuffle call

Appreciate your comments...