-
-
Save dscheg/a4c7bc9d1a9d7003d73e0a9a53e87345 to your computer and use it in GitHub Desktop.
Predicting Random output sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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