.\" ident @(#)random_shuffle.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH random_shuffle 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2random_shuffle\fP \ - Randomly shuffles elements of a collection. .SH SYNOPSIS .br #include .br template .br void random_shuffle (RandomAccessIterator first, .RE .RS 21 RandomAccessIterator last); .RE .RS 0 .br template .RE .RS 0 void random_shuffle (RandomAccessIterator first, .RE .RS 21 RandomAccessIterator last, .br RandomNumberGenerator& rand); .SH DESCRIPTION The random_shuffle algorithm shuffles the elements in the range \f2[first, last)\fP with uniform distribution. random_shuffle can take a particular random number generating function object \f2rand\fP (where \f2rand\fP takes a positive argument \f2n\fP of distance \f2type\fP of the \f2RandomAccessIterator\fP) and returns a randomly chosen value between \f20 \fPand\f2 n - 1\fP. .SH COMPLEXITY In the random_shuffle algorithm, \f2(last - first) -1 \fPswaps are done. .SH EXAMPLE .RE .RS 0 // .br // rndshufl.cpp .br // .RE .RS 1 #include .br #include .br #include .RE .RS 0 using namespace std; .br int main() .RE .RS 1 { .RE .RS 3 //Initialize a vector with an array of ints .RE .RS 2 int arr[10] = {1,2,3,4,5,6,7,8,9,10}; .br vector v(arr, arr+10); .RE .RS 3 //Print out elements in original (sorted) order .RE .RS 2 cout << "Elements before random_shuffle: " << endl .RE .RS 8 << " "; .RE .RS 2 copy(v.begin(),v.end(), .RE .RS 7 ostream_iterator(cout," ")); .RE .RS 2 cout << endl << endl; .RE .RS 3 //Mix them up with random_shuffle .br random_shuffle(v.begin(), v.end()); .br //Print out the mixed up elements .RE .RS 2 cout << "Elements after random_shuffle: " << endl << " "; .br copy(v.begin(),v.end(), .RE .RS 7 ostream_iterator(cout," ")); .RE .RS 2 cout << endl; .RE .RS 0 .RE .RS 2 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br Elements before random_shuffle: .RE .RS 4 1 2 3 4 5 6 7 8 9 10 .RE .RS 0 Elements after random_shuffle: .RE .RS 4 7 9 10 3 2 5 4 8 1 6 .SH WARNINGS If your compiler does not support default template parameters, you always need to supply the \f2Allocator\fP template argument. For instance, you need to write: \f2vector >\fP instead of: \f2vector\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP.