+ 8

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

22nd Jan 2020, 2:28 PM
bell
bell - avatar
6 Antworten
+ 7
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
22nd Jan 2020, 4:17 PM
RKK
RKK - avatar
22nd Jan 2020, 3:52 PM
Hatsy Rei
Hatsy Rei - avatar
+ 2
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?
22nd Jan 2020, 4:24 PM
bell
bell - avatar
+ 2
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!
22nd Jan 2020, 4:53 PM
bell
bell - avatar
+ 1
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?
24th Jan 2020, 7:31 AM
Esteban Chacon Martín
Esteban Chacon Martín - avatar
0
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
24th Jan 2020, 8:18 AM
bell
bell - avatar