Random_shuffle algorithm - identical results obtained without random generator function?

If a random generator function is not fed into the random_shuffle algorithm in the standard library, will successive program runs create the same random sequence if they are provided with the same data?

For example, if

std::random_shuffle(filenames.begin(), filenames.end()); 

is executed in the same list of file names from the directory in sequential runs of the program, is the random sequence obtained in the same way as in the previous run?

+6
source share
3 answers

25.2.11 simply says that the elements are shuffled with a uniform distribution. It makes no guarantees as to which RNG is used behind the scenes (unless you pass it), so you cannot rely on this behavior.

To guarantee the same result in random order, you need to provide your own RNG, which provides these guarantees, but I suspect that even if you update the standard library, the random_shuffle algorithm itself can change the effects.

+6
source

If you use the same random generator, with the same seed and the same start sequence, the results will be the same. A computer, after all, is determined in its behavior (modulo problems with flows and several other coefficients and ends).

If you do not specify a generator, the default generator implementation is defined. Most implementations, I think, use std::rand() (which can cause problems, especially when the number of elements in a sequence is greater than RAND_MAX ). I would advise getting a generator with a known quality and its use.

If you misuse the generator that is being used (another reason not to use the default value, since how you sow it will depend on), then you will get what you get. In the case of std::rand() , the same seed is always used by default. How you seed depends on the generator used. What you use to sow it must be different from one run to another; for many applications, time(NULL) sufficient; on a Unix platform, I would recommend reading, however, it takes a lot of bytes from /dev/random . Otherwise, hashing other information (machine IP address, process ID, etc.) can also improve the situation --- this means that two users running the program in exactly the same second will still receive different sequences. (But this is really true if you work in a network environment.)

+7
source

You can get the same result every time you start the program. You can add an arbitrary random number generator (which can be sown from an external source) as an additional argument to std::random_shuffle if this is a problem. The function will be the third argument. Some people recommend calling srand(unsigned(time(NULL))); to random_shuffle , but the results are often repeated when the implementation is implemented (and unreliable).

+4
source

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


All Articles