shuffle vs random_shuffle in C++
It randomly rearrange elements in range [first, last).
The function swaps the value of each element with some other randomly picked element. When provided, the function gen determines which element is picked in every case. Otherwise, the function uses some unspecified source of randomness.
// CPP program Illustrating the // use of random_shuffle #include <bits/stdc++.h> using namespace std; // random generator function int randomfunc(int j) { return rand() % j; } int main() { srand(unsigned(time(0))); vector<int> arr; // set some values: for (int j = 1; j < 10; ++j) // 1 2 3 4 5 6 7 8 9 arr.push_back(j); // using built-in random generator random_shuffle(arr.begin(), arr.end()); // using randomfunc random_shuffle(arr.begin(), arr.end(), randomfunc); // print out content: cout << "arr contains:"; for (vector<int>::iterator i = arr.begin(); i != arr.end(); ++i) cout << ' ' << *i; cout << endl; return 0; } |
Output:
arr contains: 5 8 1 7 9 6 4 3 2
shuffle
Rearranges the elements in the range [first, last) randomly, using g as uniform random number generator.
The function swaps the value of each element with that of some other randomly picked element. The function determines the element picked by calling g().
// CPP program Illustrating // the use of shuffle #include <bits/stdc++.h> using namespace std; // Driver Program int main() { array<int, 5> s{ 1, 2, 3, 4, 5 }; // To obtain a time-based seed unsigned seed = 0; // Use of shuffle shuffle(s.begin(), s.end(), default_random_engine(seed)); cout << "shuffled elements are:"; for (int& i : s) cout << ' ' << i; cout << endl; return 0; } |
Output:
shuffled elements are: 3 1 5 4 2
What is the difference between shuffle and random_shuffle c++?
- The only difference is that random_shuffle uses rand() function to randomize the items, while the shuffle uses urng which is a better random generator, though with the particular overload of random_shuffle, we can get the same behavior (as with the shuffle).
- shuffle is an improvement over random_shuffle, and we should prefer using the formerfor better results.
- Example of Swapping Variables using both
random shuffle:template (class RandomIt, class RandomFunc) void random_shuffle(RandomIt first, RandomIt last, RandomFunc&& r) { typename iterator_traits::difference_type i, n; n = last - first; for (i = n-1; i > 0; --i) { using std::swap; swap(first[i], first[r(i+1)]); } }Shuffle:
template void shuffle(RandomIt first, RandomIt last, UniformRandomBitGenerator&& g) { typedef typename iterator_traits::difference_type diff_t; typedef uniform_int_distribution distr_t; typedef typename distr_t::param_type param_t; distr_t D; diff_t n = last - first; for (diff_t i = n-1; i > 0; --i) { using swap; swap(first[i], first[D(g, param_t(0, i))]); } }
This article is contributed by Shambhavi Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Shuffle an Array using STL in C++
- Structures in C++
- Variables in C++
- Implementing Forward Iterator in BST
- Count substrings that contain all vowels | SET 2
- Machine Learning in C++
- How can we use Comma operator in place of curly braces?
- Optimally accommodate 0s and 1s from a Binary String into K buckets
- Remove first adjacent pairs of similar characters until possible
- Find a number containing N - 1 set bits at even positions from the right
- Sort an array according to absolute difference with given value using Functors
- Rearrange characters in a string such that no two adjacent are same using hashing
- Remove duplicate elements in an Array using STL in C++
- r-Nearest neighbors



