Use `random_int` instead of `mt_rand`
I noticed that mt_rand is being called by the library on some operations.
The one I am concerned with is the one below.
https://github.com/googleapis/google-cloud-php/blob/master/Core/src/ExponentialBackoff.php#L112
I think this interferes with seeded mt_rand and will cause applications to return inconsistent results.
Is it possible to replace these with random_int which is not affected by mt_srand?
@dwsupplee @danielgsims WDYT?
We use mt_rand() because random_int() isn't available in PHP < 7.0. We could use mt_rand() as a fallback and suggest paragonie/random_compat.
$rand = function_exists('random_int')
? 'random_int'
: 'mt_rand';
$num = $rand($min, $max);
@taka-oyama Thanks for raising this issue.
Could you please help me better understand how you could see inconsistent results with mt_rand? Would that require seeding with a fixed value? Under what cases do you see this becoming an issue?
I'm open to making a change - just trying to better understand the justification as exponential backoff doesn't really need to rely on a cryptographically secure random number (as provided by random_int).
Thank you for the consideration.
Would that require seeding with a fixed value?
Yes it does.
It's a problem when we try to reproduce a specific user action.
In our case, we try to replay user activity and use it for stress testing the application.
In order to reproduce it perfectly, we fix the seed per request so that we get the same results every time. Since the results of the seeded mt_rand is determined by how many times it is called, it's important to have mt_rand be called exactly the same number of times.
We cannot assure this if any external library calls mt_rand based on external factors (network connectivity in this case).
I don't think it's a client library issue. Even if we replace mt_rand() with random_int(), there's no guarantee that no part of the library uses the same function.
If you want to get the same sequence of pseudo-random numbers every time, you have to use your own random number generator and fully control its seed. Linear congruential generators are simple and good enough for imitation of user activity. Rand function may be like
function myRand(&$seed, $min, $max)
{
$seed = ($seed * 4561 + 51349) % 243000;
return (int)($seed * ($max - $min + 1) / 243000) + $min;
}
@taka-oyama, is that suitable in your case? Can this issue be closed?
Thank you for the reply.
Yeah, we ended up writing our own PRNG class so we can control the seed in user land so I don't mind closing this.
However, could you elaborate on this
there's no guarantee that no part of the library uses the same function.
I don't quite follow what you mean.
Well, your plan was to use standard function with side effects (mt_rand()) and assume that all used parts of all used libraries call this function in predictable manner (or don't call at all). This assumption is unsafe and should never be used, because you actually rely on implementation details of codebase you cannot control. Even if current library version is predictable, this may change in future.
True.
Thank you for the explanation.
I will close this now.