Skip to content

Instantly share code, notes, and snippets.

@dscheg
Created January 28, 2018 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dscheg/a4c7bc9d1a9d7003d73e0a9a53e87345 to your computer and use it in GitHub Desktop.
Save dscheg/a4c7bc9d1a9d7003d73e0a9a53e87345 to your computer and use it in GitHub Desktop.
Predicting Random output sequence
using System;
using System.Linq;
using System.Reflection;
internal static class RndPredict
{
private static void Main()
{
var rnd = new Random(31337);
var seedArray = new[] {0}.Concat(
Enumerable.Range(0, 55)
.Select(i => rnd.Next(Min, Max))
.Select(val => GetInternalStateValue(Min, Max, val)))
.ToArray();
var predictor = new Random();
typeof(Random)
.GetField("SeedArray", BindingFlags.Instance | BindingFlags.NonPublic)
.SetValue(predictor, seedArray);
for(int i = 0; i < 10; i++)
Console.WriteLine($"{rnd.Next(Min, Max)} vs {predictor.Next(Min, Max)}");
}
private static int GetInternalStateValue(int minValue, int maxValue, int value)
{
var range = maxValue - minValue;
return (int)((double)(value - minValue) / range * int.MaxValue);
}
private const int Min = 100000;
private const int Max = 1000000;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment