My goal is to have a seeded PRNG that generates the same numbers both in Javascript and in PHP. I found a linear congruential generator that works in both environments, but my problem is that if I use a division to get it in the range I need, Javascript and PHP's different floating precision means the numbers will be slightly different.
I'm not really sure if it would be a problem in my specific case (I don't need numbers higher than 100), but to get around it I tried using the modulo operator. As I expected, since the numbers that the PRNG generates are relatively small, the probability of each number coming out gets smaller as the numbers get higher.
Are there any (possibly simple) algorithms that would work reliably both client and server side?
This is the generator in Javascript that uses the modulo operator to get the integers in range, along with some code that demonstrates that higher numbers come out less frequently
function SeededRandom(newSeed) {
this.seed = newSeed;
this.Random = function (max) {//max value not included
this.seed = (this.seed * 20077 + 12345) % 32768;
return this.seed % max;
}
}
const MAX_ITER = 1000000;
const MAX_VALUE = 10000;
let random = new SeededRandom(123456);
let array = new Array(MAX_VALUE).fill(0);
for (let iii = 0; iii < MAX_ITER; iii++) {
array[random.Random(MAX_VALUE)]++;
}
for (let iii = 0; iii < MAX_VALUE; iii++) {
console.log(iii + ". " + (array[iii] / MAX_ITER));
}