Until recently, I knew that rand() in C++ works awfully with random_shuffle and it was not credible, but continued to use it for most tasks.. But recently I wrote some code and I cannot explain the result. Maybe you can?
I advise you to read to the end, it is really very interesting!
At each step, this code generates a random value from 0 to 1. As soon as 11 (for example) identical values equal 1 were received in a row, index number of current value is added to the vector. This operation is repeated a number of times and each time it starts from the beginning.
Let's output vector values and look at the last 15 of them for convenience.
Code here:
At first glance it seems that the values are random. But let's try to sort the vector.
Code here:
It should be noted that the output is different each time, but the individual values are the same. Moreover, these individual values are repeated three to four times.
Using of mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); instead of rand() gives a random output array.
If we add a line that will not affect the answer in any way, but will affect the time difference between the rand() calls we also get random values in the output array. In doing so, we have to use random delay. If the delay is fixed, we will not get random values.
With a fixed delay, we also get magic values, even if the delay is large.
Now let's instead of an empty loop in the third code call rand().
Values become repeated much more often..
If we call the method twice, the values are repeated more often.
I believe that this is not quite a trivial algorithm and the result is rather strange. If you have any idea how to explain this, please state your thoughts in the comments. Thanks for reading!