This class is able to predict the random numbers generated by the legacy random number generator in .NET (based on the workings of the Random class in .NET framework 4.8 as described here). The legacy version of the random number generator is used when:
- Your code targets .NET verions prior to .NET 6.0
- Your code targets .NET 6.0 or newer and you passed a
seed
into the constructor of theRandom
object.
A pseudo random number generator generates numbers based on an internal state. If the internal state is known, it is possible to predict the next numbers that will be generated. The RandCrack class is able to determine the internal state of the .NET legacy random number generator, and use this information to predict the numbers that will be generated in the future.
In order to use RandCrack you have to create a RandCrack
object and give it an array of at least 55 consecutively generated 32 bit numbers. These 55 numbers are necessary to determine the state of the generator. After that, you can predict the coming numbers in the same way as you would generate them with a Random
object.
Random r = new Random(secretseed);
// Generate at least 55 numbers, these are used to determine the state of the generator
int[] series = new int[55];
for(int i = 0 ; i< series.Length ; i++)
{
series[i] = r.Next();
}
RandCracker rc = new RandCracker(series);
int pred = rc.PredictNext();
int actual = r.Next();
Console.WriteLine("Random result " + actual + " | Predicted result " + pred);