jeudi 5 mai 2016

Prediciting next numbers in a .Net random sequence

What I am trying to do is to predict the next number in a series of integers generated by System.Random from an existing sequence of 56+ random integers.

I know that System.Random uses the subtractive algorithm from D. E. Knuth's book and that given the last 55 integers, the next integer in the sequence will be:

seq[n] = (seq[n-55] - seq[n-24]) % m

So I tried to populate a random list and find m with brute force.

        var rnd = new Random();
        var list = new List<int>();
        for(var i=0; i< 56; i++)
            list.Add(rnd.Next());

        var n1 = list[0];
        var n2 = list[31];
        var n = list[55];

        Console.WriteLine("{0} = ({1} - {2}) % m", n, n1, n2);
        for(var i = 1; i< int.MaxValue; i++)
        {
            if (n == (n1 - n2) % i)
                Console.WriteLine("m = {0}", i);
        }

It did not work. I then noticed an apparent bug in System.Random implementation where they used 34 instead of 24. I tried that also, again with no luck.

I would really appreciate if someone could tell me what I am doing wrong in here.




Aucun commentaire:

Enregistrer un commentaire