Use of std::shuffle
std::shuffle
is a function in the C++ standard library used to randomly arrange (shuffle) elements in containers. Its implementation is based on modern random number generators, so it is morestd::random_shuffle
More secure and flexible (std::random_shuffle
Remove after C++14 is deprecated and removed after C++17).
1. Syntax
#include <algorithm> #include <random> std::shuffle(RandomIt first, RandomIt last, URBG&& g);
-
first
andlast
: Indicates the range to be randomly disrupted ([first, last)
)。 -
g
: Random number generator must comply with UniformRandomBitGenerator (such asstd::mt19937
)。 - Return value: None (the function will directly modify the content of the input range).
2. Use examples
#include <iostream> #include <vector> #include <algorithm> #include <random> // Need to include the <random> header file int main() { std::vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // Create a random number generator std::random_device rd; // Random device (hardware entropy source) std::mt19937 g(rd()); // Mason's rotation algorithm (commonly used random number engine) // Chaotic order std::shuffle((), (), g); // The result after the output is disrupted std::cout << "Shuffled vector: "; for (int num : vec) { std::cout << num << " "; } std::cout << std::endl; return 0; }
3. Code parsing
-
Create a random number generator
-
std::random_device rd;
: Used to generate seeds. -
std::mt19937 g(rd());
: Use the Mason rotation algorithm (Mersenne Twister) as the pseudo-random number engine.
-
-
Call
std::shuffle
Reshuffle-
std::shuffle((), (), g);
Redisruptvec
The order of elements in .
-
-
Print the messed array
- Traversal and output
vec
。
- Traversal and output
4. Sample output
Shuffled vector: 3 7 5 9 1 4 2 8 6
(The order of output is random, and the results may be different for each run.)
5. Important Notes
Why notstd::random_shuffle
?
-
std::random_shuffle
Requires internal callrand()
, its randomness is weak, andrand()
Not thread-safe. -
std::shuffle
Allows the use of high-quality random number generators (e.g.std::mt19937
)。
std::mt19937
vs std::default_random_engine
-
std::default_random_engine
It may be different because different compilers implement differently, so it is recommended to usestd::mt19937
。
How to use fixed seeds for reproducible random shuffles?
std::mt19937 g(42); // 42 as a fixed random seedstd::shuffle((), (), g);
This way, every time you run the code, you will get the same disruption result.
6. Applicable scenarios
- Random sorting of data
- Generate random test cases
- Shrink (such as playing cards)
- Scramble data to avoid sorting bias (such as machine learning data preprocessing)
7. Conclusion
std::shuffle is a random shuffle method recommended in modern C++. Combined with std::mt19937, it can provide high-quality randomness and is suitable for various randomly arranged scenarios.
🚀 It is recommended to use std::shuffle instead of std::random_shuffle, and pair it with std::mt19937 for better randomness and controllability!
This is the article about the use of std::shuffle in C++. For more related C++ std::shuffle content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!