random_shuffle man page on SunOS

Man page or keyword search:  
man Server   20652 pages
apropos Keyword Search (all sections)
Output format
SunOS logo
[printable version]

random_shuffle(3C++)		       -		  random_shuffle(3C++)

Standard C++ Library Copyright 1998, Rogue Wave Software, Inc.

NAME
       random_shuffle

	- Randomly shuffles elements of a collection.

SYNOPSIS
       #include <algorithm>
       template <class RandomAccessIterator>
       void random_shuffle (RandomAccessIterator first,
		     RandomAccessIterator last);

template <;class RandomAccessIterator,
	 class RandomNumberGenerator>
void random_shuffle (RandomAccessIterator first,
		     RandomAccessIterator last,
		     RandomNumberGenerator& rand);

DESCRIPTION
       The random_shuffle algorithm shuffles the elements in the range [first,
       last) with uniform distribution. random_shuffle can take	 a  particular
       random number generating function object rand (where rand takes a posi‐
       tive argument n of  distance  type  of  the  RandomAccessIterator)  and
       returns a randomly chosen value between 0 and n - 1.

COMPLEXITY
       In the random_shuffle algorithm, (last - first) -1 swaps are done.

EXAMPLE
//
// rndshufl.cpp
//
 #include <algorithm>
 #include <vector>
 #include <iostream>
using namespace std;
int main()
 {
   //Initialize a vector with an array of ints
  int arr[10] = {1,2,3,4,5,6,7,8,9,10};
  vector<int> v(arr, arr+10);
   //Print out elements in original (sorted) order
  cout << "Elements before random_shuffle: " << endl
	<< "	 ";
  copy(v.begin(),v.end(),
       ostream_iterator<int,char>(cout," "));
  cout << endl << endl;
   //Mix them up with random_shuffle
   random_shuffle(v.begin(), v.end());
   //Print out the mixed up elements
  cout << "Elements after random_shuffle: " << endl << "     ";
  copy(v.begin(),v.end(),
       ostream_iterator<int,char>(cout," "));
  cout << endl;

  return 0;
 }

Program Output

Elements before random_shuffle:
    1 2 3 4 5 6 7 8 9 10
Elements after random_shuffle:
    7 9 10 3 2 5 4 8 1 6

WARNINGS
       If  your	 compiler  does	 not  support default template parameters, you
       always need to supply the Allocator template  argument.	For  instance,
       you need to write:

       vector<int, allocator<int> >

       instead of:

       vector<int>

       If  your compiler does not support namespaces, then you do not need the
       using declaration for std.

Rogue Wave Software		  02 Apr 1998		  random_shuffle(3C++)
[top]

List of man pages available for SunOS

Copyright (c) for man pages and the logo by the respective OS vendor.

For those who want to learn more, the polarhome community provides shell access and support.

[legal] [privacy] [GNU] [policy] [cookies] [netiquette] [sponsors] [FAQ]
Tweet
Polarhome, production since 1999.
Member of Polarhome portal.
Based on Fawad Halim's script.
....................................................................
Vote for polarhome
Free Shell Accounts :: the biggest list on the net