random_shuffle not random on Mac

The same code does not generate random groups when run on my mac. On the playground it does. I am compiling with g++ and I am not getting errors about missing libraries. It worries me because I use random algorithms for my work. Does anyone know why on my Mac random_shuffle gives always the same result despite setting srand? Sorry if it is trivial. I insert the code (algorithm is unfinished) as context. https://code.sololearn.com/cLdtLqqp4PgW/?ref=app

1/22/2020 2:28:20 PM


8 Answers

New Answer


Sorry I am not Mac user but can you try running this code once and see the result. I have used shuffle and not random_shuffle which is deprecated as ~ swim ~ said. And if this code meets your requirement then we can assume it's due to random_shuffle. https://code.sololearn.com/c2mqX6eJ5dX5/?ref=app


Likely related: https://stackoverflow.com/questions/22496942/


Try std::shuffle std::random_shuffle is deprecated since C++14


Thank you! I will try the alternative shuffle. I had read this stackoverflow post but also I am confused by different explanations to the same problem. One states that the ios time being a very large number causes overflow and the seed resulting in an identical number every time, so that srand(time(NULL)) should be avoided on IOS. Is that so?


RKK, I tried your code and it works on my mac. still, it is changing random_shuffle to shuffle as well as the random generator, srand to mt19937. so either could be the cause. i am curious to understand if the problem is random_shuffle or srand but the main thing is to have something working, thank you!


Probably random_shuffle use rand() function which itself is deprecated in C++ becuase it was not random enough https://en.cppreference.com/w/cpp/algorithm/random_shuffle


To make a real random value doesn't need to use a kind of "seed" or something like that?, I don't remember what lang have that method to generate a real random, that seed is use with the time function, someone can tell me what language is that?


yes, srand(time(NULL)) provides the seed but apparently not a very good one on IOS there are better alternatives, see posts from ~swim~ and RKK