The explanation has already been given by @chambelix, it seems that the mod (%)
operator does not work very well in PHP
for when the "module" of the negative divisor (read the number without the negative sign) is greater than the positive dividend ... Or vice versa, see tests:
echo (12345 % (-2147471303))."\n"; // Retorna 12345 (errado)
echo ((-2147471303) % 12345)."\n"; // Retorna -9173 (errado)
To have a better experience with very large negative values, there is a function that solves the problem:
function truemod($num, $mod) {
return ($mod + ($num % $mod)) % $mod;
}
echo truemod(12345,-2147471303)."\n"; // Retorna -2147471303 (certo)
echo truemod(-2147471303,12345); // Retorna 3172 (certo)
I honestly think the function you are using is a little confusing ... I would do it this way:
for ($i = 0; $i < 10; $i++){
$numerador = (1103515245 * $i + 12345);
$seed = truemod($numerador,-2147471303);
echo "BSD: " . $seed . "\n";
}
echo "\n";
function truemod($num, $mod) {
return ($mod + ($num % $mod)) % $mod;
}
In this case: &$seed
the &
operator functions as a reference to the $seed
variable that was passed by the parent function ...
/ * I find it kind of crazy to understand this function below, or to see a utility, but ok ... * /
function bsd_rand($seed) {
return function() use (&$seed) {
//Retorne essa função, utilizando a mesma referencia de variável que gerou a variável $seed
}
}
In general, the & $variavel
operator in this case acts as a reference ...
Useful links:
What are referrals?
What references do