I was wondering if, given a known PRNG and a set of values, one could calculate that PRNG's base seed. For example, say I have an array of data
var a = [255, 192, 0, 3, 72]
I know for a fact that each of those values was generated from calls PRNG.nextInt(). So the first call to PRNG.nextInt() returned 255, the next call returned 192, and so on. If you were only given that data set, is there a PRNG algorithm to give me the original seed? So that you could do something like this:
var seed = PRNG.getSeedForData([255, 192, 0, 3, 72])
var prng = new PRNG(seed)
And then with that prng instance, this should be true:
prng.nextInt() == 255
prng.nextInt() == 192
prng.nextInt() == 0
prng.nextInt() == 3
prnt.nextInt() == 72
I looked at this post (Is it possible to reverse a pseudo random number generator?) as well as this one (Given a RNG algorithm and a series of numbers is it possible to determine what seed would produce the series?), but I'm not sure what to do with that information. It doesn't look like either post found a solution to the problem, however, and I don't want to necropost.
Based on what little I understood from those posts, though, it looks like Linear congruential generators might be the way to go?, but I'm not entirely sure how to even approach them.
Just to clarify, I trying to write an PRNG algorithm that can also give me a seed when supplied with a series of numbers. The nextInt() function in the algorithm should ideally always return a value between 0 and 255, but I'm not sure if that is relevant to the question.
UPDATE
So using the links and advice in the comments, it looks like a reverse linear congruential generator is the way to go. I wrote a function in javascript to calculate the modulus 'm' of a LCG given a bunch of data, but I've run into a problem:
function gcd(a, b)
{
while(b != 0)
{
t = b;
b = a % b;
a = t;
}
return a;
}
var numbers = [this is any random array of 0-255 integers],
m = 0;
for(var i = 0; i < numbers.length - 3; i++)
{
var t0 = numbers[i + 1] - numbers[i],
t1 = numbers[i + 2] - numbers[i + 1],
t2 = numbers[i + 3] - numbers[i + 2],
u0 = Math.abs((t2 * t0) - (t1 * t1));
m = i == 0 ? u0 : gcd(m, u0);
}
console.log(m);
The issue is that this code always returns m = 1, which obviously isn't correct. If m is one, the linear congruential generator would just have mod 1, which will always return 0 for integers. Am I doing something wrong, or perhaps I misunderstood the other linked questions and answers. Is it possible to get a seed value from a PRNG given a completely random set of integers?
In terms of the code, the GCD function is based on Wikipedia's entry for a Euclid's Algorithm. The rest of it was based on the math in the question and answer @Dukeling linked. It looks right to me, but entirely possible that it isn't.