Let we explain what I mean.
Some time ago, while writing a program in c#, I've made following mistake:
int Randomize()
{
Random r=new Random();
return r.Next(0,10);
}
in c#, this is a mistake, because, called several times in a row, this function will return the same value. This is because Random constructor uses time seed, and the time difference between calls was too low (took me an hour to find that one :) ).
Now I'm using rand(...)
in php, and I need for the output to always be different, even if 2 scripts are executed at the same time.
Do I have to do something to get this result, or is it designed to work this way?
the
rand()
and alsomt_rand()
callssrand()
andmt_srand()
to produce always random results. But here's an interesting post on php.net:So, just call
srand
more frequently ormt_rand
.About
mt_rand()
function...From http://php.net/manual/en/function.mt-srand.php
Here is link, with description of the "Mersenne Twister(MT)" pseudorandom number generating algorithm (and implementations in C, C++, C#)
Here you can find implementation of this function in PHP 5
And in
php_rand.h
I found this:So now you can see, that random functions in PHP relies on
time
function...