TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm shuffle() function

C++ algorithm shuffle() function with c++ tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

C++ Algorithm shuffle()

C++ Algorithm shuffle() function reorders the elements of a range by putting them at random places, using g as uniform random number generator.

Syntax

template <class RandomAccessIterator, class URNG>
  void shuffle (RandomAccessIterator first, RandomAccessIterator last, URNG&& g);

Parameter

first: A random access iterator pointing the position of the first element in the range to be rearranged.

last: A random access iterator pointing the position one past the final element in the range to be rearranged.

g: A special function object called a uniform random number generator.

Return value

None

Complexity

Complexity is linear in the range [first, last): obtain random values and swaps elements.

Data races

The object in the range [first, last) are modified.

Exceptions

This function throws an exception if any of random number generations, the element swaps or an operation on iterator throws an exception.

Please note that invalid parameters cause an undefined behavior.

Example 1

Let's see the simple example to demonstrate the use of shuffle():

#include <iostream>     // std::cout
#include <algorithm>    // std::shuffle
#include <array>        // std::array
#include <random>       // std::default_random_engine
#include <chrono>       // std::chrono::system_clock

using namespace std;

int main () {
  array<int,5> foo {1,2,3,4,5};

  // obtain a time-based seed:
  unsigned seed = chrono::system_clock::now().time_since_epoch().count();

  shuffle (foo.begin(), foo.end(), default_random_engine(seed));

  cout << "shuffled elements:";
  for (int& x: foo) cout << ' ' << x;
  cout << '\n';

  return 0;
}

Output:

shuffled elements: 4 1 3 5 2

Example 2

Let's see another simple example:

#include <random>
#include <algorithm>
#include <iterator>
#include <iostream>

using namespace std;
 
int main()
{
    vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 
    random_device rd;
    mt19937 g(rd());
 
    shuffle(v.begin(), v.end(), g);
 
    copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
    cout << "\n";
    
    return 0;
}

Output:

8 6 10 4 2 3 7 1 9 5

Example 3

Let's see another simple example:

#include <algorithm>
#include <iostream>
#include <vector>
#include <numeric>
#include <iterator>
#include <random>

using namespace std;

int main() {
  vector<int> v(10);
  iota(v.begin(), v.end(), 0);

  cout << "before: ";
  copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
  cout << endl;

  random_device seed_gen;
  mt19937 engine(seed_gen());
  shuffle(v.begin(), v.end(), engine);

  cout << " after: ";
  copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
  cout << endl;
  
  return 0;
}

Output:

before: 0 1 2 3 4 5 6 7 8 9 
 after: 4 3 1 2 7 0 8 9 6 5

Next TopicC++ Algorithm




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf