0

When does the rand() function change it's seed value?

Is it possible for a second instance of php calling the function to interrupt and initiate a rand() function before the first instance of the function has changed the seed value and thereby return the same result to two seperate php script instances?

1 Answers1

0

Each instance of PHP will have its own seed value. You can change the seed value at any time by calling srand().

The random sequence for a particular seed may change from one version of PHP to the next (particularly in version 4.2.0 when automatic seeding of rand() was introduced, and version 7.1.0 when rand() was made an alias of mt_rand()).

However, as long as you're working with the same version of PHP, the sequence of numbers returned by successive calls to rand() after calling srand() with a particular value will always be exactly the same.


Update, based on your comment

But when you have an asynchronous upload of 20 image files all running the same php script and in that script is the rand() function which is used to return random number which is used to select a filename from among 16 thousand possible names and within 50 users 8 or 10 produce filenames that are the same I tend to conclude that rand() is not only not atomic but it is possible for different instances of PHP to have rand() return the same result.

  1. Your question made no mention of your duplicate filename problem, which is unlikely to have anything to do with the behaviour of rand(). I suggest you read up on the birthday paradox, which explains why n random selections from a set of 16000 items will produce duplicate selections with almost 100% certainty as n approaches 400. By the sound of things, you have 50 users uploading 20 files each, so the probability of multiple uploads being assigned to the same filename is very close to 1. Or to put it another way, the probability of having no uploads assigned to the same filename is less than one in a trillion.

  2. You can test the atomicity of rand() for yourself. Copy the following to your command line:

    for n in $(seq 1 20)
      do
        sleep 0.05
        php -r "srand(0); for (\$i=0; \$i<20000000; \$i++) rand(); echo '$n: ' . rand() . chr(10);" &
      done
    

    This creates 20 simultaneous processes, each of which generate 20 million numbers from the same seed and then outputs the last number in the sequence. If these processes interfered with one another in any way, then you would expect the outputs to be unstable. But they aren't.

  3. The answers to this question explain how to generate unique filenames for uploaded files.

r3mainer
  • 23,981
  • 3
  • 51
  • 88
  • But when you have an asynchronous upload of 20 image files all running the same php script and in that script is the rand() function which is used to return random number which is used to select a filename from among 16 thousand possible names and within 50 users 8 or 10 produce filenames that are the same I tend to conclude that rand() is not only not atomic but it is possible for different instances of PHP to have rand() return the same result. – Dennis Nolan Jun 06 '18 at 02:48