I am using a linear conguential generator in my javascript code but now I need to validate the results server side (generate the same numbers from the same seed). I have translated my javascript code in PHP but it does not work as expected. The first few numbers are close to the javascript ones but with less precision and the sequence contains some negative numbers which are not present in the javascript version. I think this is because of PHP's different floating point precision but I am confused by the negative numbers.
If there is no easy way to make this work in PHP what other methods could I use to generate the same sequence of pseudo-random numbers both in javascript and in PHP?
Javascript
function SeededRandom(newSeed) {
this.seed = newSeed;
this.Random = function (min, max) {
this.seed = (this.seed * 9301 + 49297) % 233280;
return Math.floor(min + (this.seed / 233280) * (max - min + 1));
}
}
PHP
class SeededRandom {
private $seed;
public function __construct($newSeed) {
$this->seed = $newSeed;
}
public function Random($min, $max) {
$this->seed = ($this->seed * 9301 + 49297) % 233280;
return floor($min + ($this->seed / 233280) * ($max - $min + 1));
}
}