Usage and utility std :: shuffle?

If you look at the characteristics of random shuffling in C ++ 11, there are 3 functions. My question is what are the typical uses and benefits:

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

compared with:

 template< class RandomIt > void random_shuffle( RandomIt first, RandomIt last ); 

I mean, whatever the URNG (uniform distribution), the result will be the same (from a statistical point of view). The only thing I see is that std::shuffle is safe, while this overload of std::random_shuffle not. Could you confirm this?

EDIT: I thought URNG should be uniform distribution, but this does not seem to compile. So can anyone give a small example using std::shuffle ?

+4
source share
1 answer

As mentioned in the comments, std::shuffle accepts a random number generator (or engine in standard terms), rather than a random number distribution. Different random number generators have different characteristics, even if they have a theoretically uniform distribution.

  • Random or pseudo-random . Random number generators use some kind of source of external entropy. Pseudo-random generators (PRNG) are strictly determined.
  • Performance - some generators are faster than others.
  • Memory usage - some PRNGs require more memory to store their state than others.
  • Period period - all PRNGs have a finite period after which they begin to repeat the same sequence from the very beginning. Some of them have much longer periods than others.
  • Quality of chance - there are numerous tests to measure whether there are subtle (or not very subtle!) In a pseudo-random stream. See, for example, Diehard tests .
  • Whether the stream is cryptographically secure or not. AFAIK, none of the standard PRNGs are.

For an overview of the various generators offered by the standard, see http://en.cppreference.com/w/cpp/numeric/random .

+2
source

Source: https://habr.com/ru/post/1492093/


All Articles