Esempio n. 1
0
        private static IEnumerable <Random> EnumRandoms()
        {
            var seed = Seed;

            System.Diagnostics.Trace.WriteLine("SEED: " + seed);
            yield return(new Random());

            yield return(MTRandom.Create(seed, MTEdition.Original_19937));

            yield return(MTRandom.Create(seed, MTEdition.Cok_19937));

            yield return(MTRandom.Create(seed, MTEdition.CokOpt_19937));

            yield return(MT64Random.Create(seed, MT64Edition.Original_19937));

            yield return(MT64Random.Create(seed, MT64Edition.Opt_19937));

            yield return(SfmtRandom.Create(seed, SfmtEdition.Original_19937));

            yield return(SfmtRandom.Create(seed, SfmtEdition.Opt_19937));

            yield return(DsfmtRandom.Create(seed, DsfmtEdition.Original_19937));

            yield return(DsfmtRandom.Create(seed, DsfmtEdition.Opt_19937));

            yield return(DsfmtRandom.Create(seed, DsfmtEdition.OptGen_521));

            yield return(DsfmtRandom.Create(seed, DsfmtEdition.OptGen_1279));

            yield return(DsfmtRandom.Create(seed, DsfmtEdition.OptGen_2203));

            yield return(DsfmtRandom.Create(seed, DsfmtEdition.OptGen_4253));

            yield return(DsfmtRandom.Create(seed, DsfmtEdition.OptGen_11213));

            yield return(DsfmtRandom.Create(seed, DsfmtEdition.OptGen_19937));

            yield return(DsfmtRandom.Create(seed, DsfmtEdition.OptGen_44497));

            yield return(DsfmtRandom.Create(seed, DsfmtEdition.OptGen_86243));

            yield return(DsfmtRandom.Create(seed, DsfmtEdition.OptGen_132049));

            yield return(DsfmtRandom.Create(seed, DsfmtEdition.OptGen_216091));
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            if (Debugger.IsAttached)
            {
                Console.WriteLine("***************************");
                Console.WriteLine("  Run without a debugger!");
                Console.WriteLine("***************************");
                Console.WriteLine();
            }
            //
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
            Thread.CurrentThread.Priority = ThreadPriority.Highest;
            Console.WriteLine("Wait for getting calm 3 seconds...");
            Thread.Sleep(3000);
            //
            var fmt = "   {0,23}: {1,6:f3} s (mean = {2:f10} * int.MaxValue)";
            Func <Random, long> proci = Next <Random>;
            var    seed1 = Environment.TickCount;
            var    N     = 300000000;
            double mean;

            Console.WriteLine();
            Console.WriteLine("Random.Next: N = {0:#,0}", N);
            //
            var t_null = Measure(NullRandom.Defalut, N, proci, out mean);

            Console.WriteLine(fmt, "Null", t_null, mean);
            var t_rand = Measure(new Random(), N, proci, out mean);

            Console.WriteLine(fmt, "Random", t_rand, mean);
            var t_mt32_org = Measure(MTRandom.Create(seed1, MTEdition.Original_19937), N, proci, out mean);

            Console.WriteLine(fmt, "MT19937", t_mt32_org, mean);
            var t_mt32_cok = Measure(MTRandom.Create(seed1, MTEdition.Cok_19937), N, proci, out mean);

            Console.WriteLine(fmt, "MT19937-c*k", t_mt32_cok, mean);
            var t_mt32_opt = Measure(MTRandom.Create(seed1, MTEdition.CokOpt_19937), N, proci, out mean);

            Console.WriteLine(fmt, "MT19937-c*k-opt", t_mt32_opt, mean);
            var t_mt64_org = Measure(MT64Random.Create(seed1, MT64Edition.Original_19937), N, proci, out mean);

            Console.WriteLine(fmt, "MT64-19937", t_mt64_org, mean);
            var t_mt64_opt = Measure(MT64Random.Create(seed1, MT64Edition.Opt_19937), N, proci, out mean);

            Console.WriteLine(fmt, "MT64-19937-opt", t_mt64_opt, mean);
            var t_sfmt_ptr = Measure(new SfmtRandom <unsafe_sfmt_t>(seed1), N, proci, out mean);

            Console.WriteLine(fmt, "SFMT-19937-ptr", t_sfmt_ptr, mean);
            var t_sfmt_org = Measure(SfmtRandom.Create(seed1, SfmtEdition.Original_19937), N, proci, out mean);

            Console.WriteLine(fmt, "SFMT-19937", t_sfmt_org, mean);
            var t_sfmt_opt = Measure(SfmtRandom.Create(seed1, SfmtEdition.Opt_19937), N, proci, out mean);

            Console.WriteLine(fmt, "SFMT-opt-19937", t_sfmt_opt, mean);
            var t_dsfmt_ptr = Measure(new DsfmtRandom <unsafe_dsfmt_t>(seed1), N, proci, out mean);

            Console.WriteLine(fmt, "dSFMT-19937-ptr", t_dsfmt_ptr, mean);
            var t_dsfmt_org = Measure(DsfmtRandom.Create(seed1, DsfmtEdition.Original_19937), N, proci, out mean);

            Console.WriteLine(fmt, "dSFMT-19937", t_dsfmt_org, mean);
            var t_dsfmt_opt = Measure(DsfmtRandom.Create(seed1, DsfmtEdition.Opt_19937), N, proci, out mean);

            Console.WriteLine(fmt, "dSFMT-opt-19937", t_dsfmt_opt, mean);
            var t_dsfmt_gen = Measure(DsfmtRandom.Create(seed1, DsfmtEdition.OptGen_19937), N, proci, out mean);

            Console.WriteLine(fmt, "dSFMT-opt-gen-19937", t_dsfmt_gen, mean);
            var t_accurate = Measure(new AccurateRandom(DsfmtRandom.Create(seed1, DsfmtEdition.Opt_19937)), N, proci, out mean);

            Console.WriteLine(fmt, "accurate+dSFMT-opt-19937", t_accurate, mean);
            t_dsfmt_gen = Measure(DsfmtRandom.Create(seed1, DsfmtEdition.OptGen_521), N, proci, out mean);
            Console.WriteLine(fmt, "dSFMT-opt-gen-521", t_dsfmt_gen, mean);
            t_dsfmt_gen = Measure(DsfmtRandom.Create(seed1, DsfmtEdition.OptGen_216091), N, proci, out mean);
            Console.WriteLine(fmt, "dSFMT-opt-gen-216091", t_dsfmt_gen, mean);
            //
            var buf     = new byte[sizeof(int) * 10];
            var t_cprng = Measure(new System.Security.Cryptography.RNGCryptoServiceProvider(), N, rng => {
                rng.GetBytes(buf);
                var x = 0L;
                x    += ToUInt32(buf, 0) >> 1;
                x    += ToUInt32(buf, 4) >> 1;
                x    += ToUInt32(buf, 8) >> 1;
                x    += ToUInt32(buf, 12) >> 1;
                x    += ToUInt32(buf, 16) >> 1;
                x    += ToUInt32(buf, 20) >> 1;
                x    += ToUInt32(buf, 24) >> 1;
                x    += ToUInt32(buf, 28) >> 1;
                x    += ToUInt32(buf, 32) >> 1;
                x    += ToUInt32(buf, 36) >> 1;
                return(x);
            }, out mean);

            Console.WriteLine(fmt, "RNGCrypt", t_cprng, mean);
            //
            fmt = "   {0,23}: {1,6:f3} s (mean = {2:f10})";
            Func <Random, double> procd = NextDouble <Random>;
            var seed2 = new uint[] { (uint)Environment.TickCount, (uint)Environment.WorkingSet };

            N = 200000000;
            Console.WriteLine();
            Console.WriteLine("Random.NextDouble: N = {0:#,0}", N);
            //
            t_null = Measure(NullRandom.Defalut, N, procd, out mean);
            Console.WriteLine(fmt, "Null", t_null, mean);
            t_rand = Measure(new Random(), N, procd, out mean);
            Console.WriteLine(fmt, "Random", t_rand, mean);
            t_mt32_org = Measure(MTRandom.Create(seed2, MTEdition.Original_19937), N, procd, out mean);
            Console.WriteLine(fmt, "MT19937", t_mt32_org, mean);
            t_mt32_cok = Measure(MTRandom.Create(seed2, MTEdition.Cok_19937), N, procd, out mean);
            Console.WriteLine(fmt, "MT19937-c*k", t_mt32_cok, mean);
            t_mt32_opt = Measure(MTRandom.Create(seed2, MTEdition.CokOpt_19937), N, procd, out mean);
            Console.WriteLine(fmt, "MT19937-c*k-opt", t_mt32_opt, mean);
            t_mt64_org = Measure(MT64Random.Create(seed1, MT64Edition.Original_19937), N, procd, out mean);
            Console.WriteLine(fmt, "MT64-19937", t_mt64_org, mean);
            t_mt64_opt = Measure(MT64Random.Create(seed1, MT64Edition.Opt_19937), N, procd, out mean);
            Console.WriteLine(fmt, "MT64-19937-opt", t_mt64_opt, mean);
            t_sfmt_ptr = Measure(new SfmtRandom <unsafe_sfmt_t>(seed2), N, procd, out mean);
            Console.WriteLine(fmt, "SFMT-19937-ptr", t_sfmt_org, mean);
            t_sfmt_org = Measure(SfmtRandom.Create(seed2, SfmtEdition.Original_19937), N, procd, out mean);
            Console.WriteLine(fmt, "SFMT-19937", t_sfmt_org, mean);
            t_sfmt_opt = Measure(SfmtRandom.Create(seed2, SfmtEdition.Opt_19937), N, procd, out mean);
            Console.WriteLine(fmt, "SFMT-opt-19937", t_sfmt_opt, mean);
            t_dsfmt_ptr = Measure(new DsfmtRandom <unsafe_dsfmt_t>(seed2), N, procd, out mean);
            Console.WriteLine(fmt, "dSFMT-19937-ptr", t_dsfmt_ptr, mean);
            t_dsfmt_org = Measure(DsfmtRandom.Create(seed2, DsfmtEdition.Original_19937), N, procd, out mean);
            Console.WriteLine(fmt, "dSFMT-19937", t_dsfmt_org, mean);
            t_dsfmt_opt = Measure(DsfmtRandom.Create(seed2, DsfmtEdition.Opt_19937), N, procd, out mean);
            Console.WriteLine(fmt, "dSFMT-opt-19937", t_dsfmt_opt, mean);
            t_dsfmt_gen = Measure(DsfmtRandom.Create(seed2, DsfmtEdition.OptGen_19937), N, procd, out mean);
            Console.WriteLine(fmt, "dSFMT-opt-gen-19937", t_dsfmt_gen, mean);
            t_accurate = Measure(new AccurateRandom(DsfmtRandom.Create(seed2, DsfmtEdition.Opt_19937)), N, procd, out mean);
            Console.WriteLine(fmt, "accurate+dSFMT-opt-19937", t_accurate, mean);
            t_dsfmt_gen = Measure(DsfmtRandom.Create(seed2, DsfmtEdition.OptGen_521), N, procd, out mean);
            Console.WriteLine(fmt, "dSFMT-opt-gen-521", t_dsfmt_gen, mean);
            t_dsfmt_gen = Measure(DsfmtRandom.Create(seed2, DsfmtEdition.OptGen_216091), N, procd, out mean);
            Console.WriteLine(fmt, "dSFMT-opt-gen-216091", t_dsfmt_gen, mean);
            //
            buf     = new byte[sizeof(ulong) * 10];
            t_cprng = Measure(new System.Security.Cryptography.RNGCryptoServiceProvider(), N, rng => {
                rng.GetBytes(buf);
                var x = 0d;
                x    += ToDouble(buf, 0);
                x    += ToDouble(buf, 8);
                x    += ToDouble(buf, 16);
                x    += ToDouble(buf, 24);
                x    += ToDouble(buf, 32);
                x    += ToDouble(buf, 40);
                x    += ToDouble(buf, 48);
                x    += ToDouble(buf, 56);
                x    += ToDouble(buf, 64);
                x    += ToDouble(buf, 72);
                return(x);
            }, out mean);
            Console.WriteLine(fmt, "RNGCrypt", t_cprng, mean);
        }
        public void Sequence_MT64()
        {
            var seed = Environment.TickCount;

            Test(MT64Random.Create(seed, MT64Edition.Original_19937), MT64Random.Create(seed, MT64Edition.Opt_19937));
        }
Esempio n. 4
0
        //public RandomGenerator()
        //{
        //    Seed(0);
        //    SetGeneratorType(RandomGenerators.MersenneTwister);
        //}

        //public void DebugState(out int rIndex, out int rGeneration, out int rRefillCount)
        //{
        //    rIndex = mIndex;
        //    rGeneration = mRotationCount;
        //    rRefillCount = mRefillCounter;
        //}

        //public int DebugNRandomNumbers()
        //{
        //    return mIndex + RandomGeneratorSize * mRotationCount + RandomGeneratorSize * mRefillCounter;
        //}

        /// call this function to check if we need to create new random numbers.
        /// this function is not reentrant! (e.g. call every year in the model)
        public void CheckGenerator()
        {
            if (mRotationCount > 0)
            {
                // BUGBUG: check mRotationCount < RANDOMGENERATORROTATIONS
                lock (mBuffer)               // serialize access
                {
                    if (mRotationCount <= 0) // another thread might have already refilled....
                    {
                        return;
                    }

                    RandomGenerators generator = new();
                    generator.SetSeed(mBuffer[RandomByteBufferSize + 4]); // use the last value as seed for the next round....
                    switch (mGeneratorType)
                    {
                    case RandomGeneratorType.MersenneTwister:
                    {
                        Random mersenne = MT64Random.Create(mBuffer[RandomByteBufferSize + 4]);
                        // qDebug() << "refill random numbers. seed" <<mBuffer[RANDOMGENERATORSIZE+4];
                        for (int i = 0; i < RandomByteBufferSize + 5; ++i)
                        {
                            mBuffer[i] = mersenne.Next();
                        }
                        break;
                    }

                    case RandomGeneratorType.WellRng512:
                    {
                        for (int i = 0; i < RandomByteBufferSize + 5; ++i)
                        {
                            mBuffer[i] = generator.RandomFunction(0);
                        }
                        break;
                    }

                    case RandomGeneratorType.XorShift96:
                    {
                        for (int i = 0; i < RandomByteBufferSize + 5; ++i)
                        {
                            mBuffer[i] = generator.RandomFunction(1);
                        }
                        break;
                    }

                    case RandomGeneratorType.Fast:
                    {
                        for (int i = 0; i < RandomByteBufferSize + 5; ++i)
                        {
                            mBuffer[i] = generator.RandomFunction(2);
                        }
                        break;
                    }
                    } // switch

                    mIndex         = 0; // reset the index
                    mRotationCount = 0;
                    //mRefillCounter++;
                }
            }
        }
 public void Random_MT64()
 {
     Test(MT64Random.Create(MT64Edition.Original_19937));
     Test(MT64Random.Create(MT64Edition.Opt_19937));
 }