0
Why when I put srand(time(0)); before loop the result change?
Why when I put srand(time(0)); before loop the result change? Yes I know it should change but when you put before like srand(time(0)); for(int x=0; x<=5;x++){... I get like input 6 Random numbers but when I write for(int x=0; x<=5;x++){ srand(time(0));... I get the same 6 Random numbers?
1 ответ
+ 2
Well, random values is one of the things that is hard for a computer.
rand() returns a psedo random result, it apears to bee random.
But starting from the same state it will return the same sequenze again and again.
With srand() you set the starting point for rand().
As time(0) returns the number of seconds sense 1:st january 1970, in the second version, you feed srand() with the same number and rand() will return the same result.
See my code rand() srand() demo.