Perl's rand() function with localtime as seed

Question!

Please look at the following piece of code:

srand(localtime);

for (my $ik = 0; $ik < 3; $ik += 1)
{
    print int(rand(10)),"\n";
    sleep(1);
}

I invoke the above piece of code multiple times with sufficient time(5-10 sec) in between, still the output sequence is the same.

As I have set the seed to localtime every invocation must use a different seed and perhaps generate a different sequence of three numbers, because of the time gap. Why do I get the same sequence again and again.

Note: The code is NOT in a loop, it is in a Perl file which is being executed multiple times.

The documentation says that this seed fails if multiple instances run in the same 'second' leading to the same seed - here it is not the case.

EDIT:: The solution by @simbabque does help but the randomness expected is not gained. Look at my comment on the aforementioned solution below.



This video can help you solving your question :)
By: admin