Is rand () time dependent in php?

Let's explain what I mean.

Some time ago, when I wrote a program in C #, I made the following mistake:

int Randomize()
{
    Random r=new Random();
    return  r.Next(0,10);
}

in C #, this is an error because, called several times in a row, this function returns the same value. This is because the Random constructor uses a temporary seed, and the time difference between calls was too low (it took me an hour to find this :)).

Now I use rand(...)in php and I need the output to always be different, even if two scripts are running at the same time.

Do I need to do something to get this result, or is it designed to work this way?

+5
source share
2 answers

rand(), mt_rand() srand() mt_srand() . php.net:

, , -, , , . srand() mt_rand(), .

, srand mt_rand.

+9

mt_rand()...

http://php.net/manual/en/function.mt-srand.php

PHP 4.2.0, srand() mt_srand(), .

( PHP 5.2.1) Mersenne Twister PHP . , . , , , , .

, Mersenne Twister (MT) ( C, ++, #)

PHP 5

php_rand.h :

#ifdef PHP_WIN32
#define GENERATE_SEED() (((long) (time(0) * GetCurrentProcessId())) ^ ((long) (1000000.0 * php_combined_lcg(TSRMLS_C))))
#else
#define GENERATE_SEED() (((long) (time(0) * getpid())) ^ ((long) (1000000.0 * php_combined_lcg(TSRMLS_C))))
#endif

, , PHP time...

+3

All Articles