public void CompareSequencesOfMersennes() { Random rnd = new Random(); for (int i = 0; i < 10; i++) { int seed = rnd.Next(); var m1 = new MT19937(); m1.init_genrand((ulong)seed); var m2 = new RandomMT((ulong)seed); var m3 = new SharpDevs.Randomization.MersenneTwister(seed); var m4 = new Random(seed); for (int j = 0; j < 10; j++) { var r1 = m1.genrand_int32(); var r2 = m2.RandomInt(); var r3 = m3.GetNextULong(); var r4 = m4.Next(); Console.WriteLine("{0}\t{1}\t{2}\t{3}", r1, r2, r3, r4); //Assert.AreEqual(r1, r2); //Assert.AreEqual(r2, r3); } Console.WriteLine(); } }
public void CompareDistributionOfMersennes() { Random rnd = new Random(); int baseSeed = rnd.Next(); var m1 = new MT19937(); m1.init_genrand((ulong)baseSeed); var m2 = new RandomMT((ulong)baseSeed); var m3 = new SharpDevs.Randomization.MersenneTwister(baseSeed); var m4 = new Random(baseSeed); var maxRange = 1000; var iterations = 10000000; var dist1 = CalculateDistribution(() => m1.RandomRange(0, maxRange - 1), maxRange, iterations); var dist2 = CalculateDistribution(() => m2.RandomRange(0, maxRange - 1), maxRange, iterations); var dist3 = CalculateDistribution(() => m3.GetNext(0, maxRange), maxRange, iterations); var dist4 = CalculateDistribution(() => m4.Next(0, maxRange), maxRange, iterations); var distribution1 = AnalyzeDistribution(dist1, iterations); var distribution2 = AnalyzeDistribution(dist2, iterations); var distribution3 = AnalyzeDistribution(dist3, iterations); var distribution4 = AnalyzeDistribution(dist4, iterations); Console.WriteLine("{0}\t{1}\t{2}\t{3}", distribution1, distribution2, distribution3, distribution4); var expectedFill = (decimal)iterations / maxRange; Console.WriteLine("Przy oczekiwanym napełnieniu: " + expectedFill); Console.WriteLine("Co daje procentowo:"); Console.WriteLine("{0}\t{1}\t{2}\t{3}", distribution1 / expectedFill * 100, distribution2 / expectedFill * 100, distribution3 / expectedFill * 100, distribution4 / expectedFill * 100); }
public static void Main(string[] args) { var mt = new MT19937(); mt.Seed(new ulong[] { 0x12345UL, 0x23456UL, 0x34567UL, 0x45678UL }); var i = 0; foreach (ulong want in expectedInt) { var have = mt.UInt64(); if (have != want) { Console.WriteLine("wrong output {0}: {1} != {2}", i, have, want); return; } i++; } i = 0; foreach (string want in expectedReal) { var have = mt.Real2().ToString("0.00000000"); if (have != want) { Console.WriteLine("wrong output {0}: {1} != {2}", i, have, want); return; } i++; } Console.WriteLine("done"); }
private byte[] PadPasswordByte(string password,int blockSize) { MT19937 m = new MT19937(Seed); while (password.Length%blockSize!=0) { password+=(char)System.Convert.ToByte(m.genrand_int31()); } byte[] ret = new byte[password.Length]; for (int x=0;x<ret.Length;x++) { ret[x]=(byte)password[x]; } return ret; }
/* generates a random number on (0,1)-real-interval */ static public double Real3() { return(((double)(MT19937.UInt64() >> 12) + 0.5) * (1.0 / 4503599627370496.0)); }
/* generates a random number on [0,1)-real-interval */ static public double Real2() { return((double)(MT19937.UInt64() >> 11) * (1.0 / 9007199254740992.0)); }
/* generates a random number on [0,1]-real-interval */ static public double Real1(bool isLog = false, int logLevel = 0) { return((MT19937.UInt64(isLog, logLevel) >> 11) * (1.0 / 9007199254740991.0)); }
/* generates a random number on [0, 2^63-1]-interval */ static public long Int63() { return((long)(MT19937.UInt64() >> 1)); }