public void InitializeRNG(int seed, Type rngType = null) { Seed = seed; if (rngType != null) { _rngType = rngType; } var nextMethod = _rngType.GetMethods(BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(m => m.Name.Equals("Next") && m.GetParameters().Length == 0 && m.ReturnType == typeof(int)); if (nextMethod == null) { throw NotImplemented("int Next()"); } var nextmaxMethod = _rngType.GetMethods(BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(m => m.Name.Equals("Next") && m.GetParameters().Length == 1 && m.GetParameters()[0].ParameterType == typeof(int) && m.ReturnType == typeof(int)); if (nextmaxMethod == null) { throw NotImplemented("int Next(int max)"); } var nextminmaxMethod = _rngType.GetMethods(BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(m => m.Name.Equals("Next") && m.GetParameters().Length == 2 && m.GetParameters().All(x => x.ParameterType == typeof(int)) && m.ReturnType == typeof(int)); if (nextminmaxMethod == null) { throw NotImplemented("int Next(int min, int max)"); } var nextdoubleMethod = _rngType.GetMethods(BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(m => m.Name.Equals("NextDouble") && m.GetParameters().Length == 0 && m.ReturnType == typeof(double)); if (nextdoubleMethod == null) { throw NotImplemented("double NextDouble()"); } var nextbytesMethod = _rngType.GetMethods(BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(m => m.Name.Equals("NextBytes") && m.GetParameters().Length == 1 && m.GetParameters()[0].ParameterType == typeof(byte[]) && m.ReturnType == typeof(void)); if (nextbytesMethod == null) { throw NotImplemented("void NextBytes(byte[] buf)"); } object[] args = { seed }; _random = Activator.CreateInstance(_rngType, args); Next = (min, max) => (int)nextMethod.Invoke(_random, new object[] { }); NextMax = (min, max) => (int)nextmaxMethod.Invoke(_random, new object[] { min }); NextMinMax = (min, max) => (int)nextminmaxMethod.Invoke(_random, new object[] { min, max }); NextDouble = () => (double)nextdoubleMethod.Invoke(_random, new object[] { }); NextBytes = bytes => nextdoubleMethod.Invoke(_random, new object[] { bytes }); Initialized = true; }
public static int Main() { RandomNextBytes test = new RandomNextBytes(); TestLibrary.TestFramework.BeginTestCase("RandomNextBytes"); if (test.RunTests()) { TestLibrary.TestFramework.EndTestCase(); TestLibrary.TestFramework.LogInformation("PASS"); return(100); } else { TestLibrary.TestFramework.EndTestCase(); TestLibrary.TestFramework.LogInformation("FAIL"); return(0); } }
public static int Main() { RandomNextBytes test = new RandomNextBytes(); TestLibrary.TestFramework.BeginTestCase("RandomNextBytes"); if (test.RunTests()) { TestLibrary.TestFramework.EndTestCase(); TestLibrary.TestFramework.LogInformation("PASS"); return 100; } else { TestLibrary.TestFramework.EndTestCase(); TestLibrary.TestFramework.LogInformation("FAIL"); return 0; } }