I have a function in my code that uses a seed to get a predictable (based on the seed) set of random numbers.
$seed = 1234;
mt_srand($seed);
echo mt_rand(0,10);
echo mt_rand(0,10);
echo mt_rand(0,10);
echo mt_rand(0,10);
Is it necessary to call mt_srand() at the end of my function to reset it back to a random seed once I'm done with it?
Yes. The state of mt_rand()
is global — if your function sets it to a fixed value, it will stay in that state after your function exits. While it's not ideal to call mt_srand()
, as that would disrupt any other function that's trying to set a fixed seed, it's still preferable to leaving the random number generator in a non-random state.