简体   繁体   中英

First call of rand() without seed() --> same number?

I am looking through ac program that is available for several OSs and produces inconsistent results among platforms for the exact same data. Looking because I lack some libraries to compile and debug it on my system.

I noticed that it calls rand() during initialization without calling srand(), probably because it needs only 1 random value. Now, I was wondering how does rand() behave in this case? Does it takes the operating system initial value of the random library ? I am currently hoping that this might explain the different results per platform, but I lack background knowledge to rand() and what happens if it is called without srand().

If it always returns an default-init value of the OS, it would explain why different platforms produces different results.

Thx for any input and sorry for the theoretic question - no code ^^.

From rand() 's manpage:

If no seed value is provided, the rand() function is automatically seeded with a value of 1.

So, in a given system, it will always give you the same sequence. However, even if the seed is the same, different platforms may implement different random generators, and thus yield different sequences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM