问题
If you look to the specifications of random shuffle in C++11, there are 3 functions. My question is what is the typical use and advantage of :
template< class RandomIt, class URNG >
void shuffle( RandomIt first, RandomIt last, URNG&& g );
compared to:
template< class RandomIt >
void random_shuffle( RandomIt first, RandomIt last );
I mean, it seems that whatever URNG is (a uniform distribution), the result will be the same (from a statistical point of view). The only point I see, is that std::shuffle
is thead-safe, whereas this overload ofstd::random_shuffle
is not. Could you confirm that ?
EDIT: I thought that URNG should be a uniform distribution but that does not seem to compile. So can someone provide a little example of use of std::shuffle
?
回答1:
As mentioned in the comments, std::shuffle
takes a random number generator (or engine in standard speak), not a random number distribution. Different random number generators have different characteristics even if they have a theoretically uniform distribution.
- Random or pseudo-random - True random number generators use some sort of an external entropy source. Pseudo-random generators (PRNGs) are strictly deterministic.
- Performance - some generators are faster than others.
- Memory usage - some PRNGs need more memory to store their state than others.
- Period length - all PRNGs have a finite period after which they start repeating the same sequence from the beginning. Some have much longer periods than others.
- Randomness quality - there are numerous tests for measuring whether there are subtle (or not-so-subtle!) patterns in a pseurorandom stream. See, for example, the Diehard tests.
- Whether the stream is cryptographically secure or not. AFAIK, none of the standard PRNGs are.
For an overview of the different generators offered by the standard, please refer to http://en.cppreference.com/w/cpp/numeric/random.
来源:https://stackoverflow.com/questions/17724876/use-and-utility-of-stdshuffle