PHP rand()
, like all psuedo-random number generators, does not generate truly random data, but instead uses some mathematical function to create seemingly random data.
The methods may vary but the basic principle is this:
We take an initial value as a 'seed', s
. (This may be the current time, a user inputted "random" value, or even true random entropy harvested from some entropy source)
We then take s
and apply a function, f
, to it producing a new value s'
(s-prime) (this function is often a modulo function or hashing function)
We can then repeat this process ad infinitum, putting s'
into f
producing s''
, etc.
This sequence of results from f
are our "random" numbers.
But, hopefully you realized, f
remained the same and will always produce the same output given the same input.
So if we give it the same starting seed s
, we will always get the same sequence of s'
,s''
,s'''
...
This is why we call them "Psuedo" random. If you don't know the starting seed and don't observe the sequence for too long, it seems random. But if we know the function f
used and the starting seed s
we can predict every value rand()
would produce.