srand() initializes the random generator to start a certain point in the (long) "list of random numbers".
If you start your program with e.g: srand(19369) you will get the same series of random numbers every time the program runs. This can be handy e.g. for some tests or debugging.
So to get different random numbers each time the program runs, time() is used to initialize the random generator. And the random numbers you get are (to a certain extend) unpredictable. time() returns the number of seconds since 1/1/1970.
In your program you call srand(time())
each time in the loop. Every time the loop runs within one second, time() will return the same number. So you're then initializing the random generator to the same starting point every tim just before you call random().
The right spot to call srand(time()) is at the start of your program. Only once.
I don't the reason why this doesn't work like that under Windows. Possible causes could be:
- Windows ignores the number passed to srand()
- Under Windows time() returns milliseconds instead of seconds under linux.
- Windows not fast enough to rum the loop within a second. :-)
[off-topic side note]
Now that time() is mentioned, recently the unix time was:
1234567890.
Code:
$ date -d @1234567890
Sa 14. Feb 00:31:30 CET 2009