天天看点

Net中随机函数的算法

以前写程序的时候,经常用到随机函数。不过也就事随手拿了一用完成目的而已。不知道今天怎么了特别想看看随机函数的算法是怎么回事。于是结合

<a href="http://dotnet.di.unipi.it/Content/sscli/docs/doxygen/fx/bcl/random_8cs-source.html">http://dotnet.di.unipi.it/Content/sscli/docs/doxygen/fx/bcl/random_8cs-source.html</a>

归纳了一下:

using System;

Net中随机函数的算法
Net中随机函数的算法

    class MyRnd

Net中随机函数的算法
Net中随机函数的算法

    ...{

Net中随机函数的算法

        private const int MBIG = Int32.MaxValue;

Net中随机函数的算法

        private const int MSEED = 161803398;

Net中随机函数的算法

        private const int MZ = 0;

Net中随机函数的算法

        private int inext, inextp;

Net中随机函数的算法

        private int[] SeedArray = new int[56];

Net中随机函数的算法
Net中随机函数的算法
Net中随机函数的算法

        public MyRnd()

Net中随机函数的算法

            : this(Environment.TickCount)

Net中随机函数的算法

        //一个 32 位带符号整数,它包含自上次启动计算机以来所经过的时间(以毫秒为单位)。

Net中随机函数的算法
Net中随机函数的算法

        ...{

Net中随机函数的算法

        }

Net中随机函数的算法
Net中随机函数的算法
Net中随机函数的算法

        public MyRnd(int Seed)

Net中随机函数的算法
Net中随机函数的算法
Net中随机函数的算法

            int ii;

Net中随机函数的算法

            int mj, mk;

Net中随机函数的算法
Net中随机函数的算法

            //Initialize our Seed array.

Net中随机函数的算法

            //This algorithm comes from Numerical Recipes in C (2nd Ed.)

Net中随机函数的算法

            mj = MSEED - Math.Abs(Seed);

Net中随机函数的算法

            SeedArray[55] = mj;

Net中随机函数的算法

            mk = 1;

Net中随机函数的算法

            for (int i = 1; i &lt; 55; i++)

Net中随机函数的算法
Net中随机函数的算法

            ...{  //Apparently the range [1..55] is special (Knuth) and so we're wasting the 0'th position.

Net中随机函数的算法

                ii = (21 * i) % 55;

Net中随机函数的算法

                SeedArray[ii] = mk;

Net中随机函数的算法

                mk = mj - mk;

Net中随机函数的算法

                if (mk &lt; 0) mk += MBIG;

Net中随机函数的算法

                mj = SeedArray[ii];

Net中随机函数的算法

            }

Net中随机函数的算法

            for (int k = 1; k &lt; 5; k++)

Net中随机函数的算法
Net中随机函数的算法

            ...{

Net中随机函数的算法

                for (int i = 1; i &lt; 56; i++)

Net中随机函数的算法
Net中随机函数的算法

                ...{

Net中随机函数的算法

                    SeedArray[i] -= SeedArray[1 + (i + 30) % 55];

Net中随机函数的算法

                    if (SeedArray[i] &lt; 0)

Net中随机函数的算法

                        SeedArray[i] += MBIG;

Net中随机函数的算法

                }

Net中随机函数的算法
Net中随机函数的算法

            inext = 0;

Net中随机函数的算法

            inextp = 21;

Net中随机函数的算法

            Seed = 1;

Net中随机函数的算法
Net中随机函数的算法
Net中随机函数的算法

        //产生0~1的double数

Net中随机函数的算法

        protected virtual double Sample()

Net中随机函数的算法
Net中随机函数的算法
Net中随机函数的算法

            int retVal;

Net中随机函数的算法

            int locINext = inext;

Net中随机函数的算法

            int locINextp = inextp;

Net中随机函数的算法
Net中随机函数的算法

            if (++locINext &gt;= 56) locINext = 1;

Net中随机函数的算法

            if (++locINextp &gt;= 56) locINextp = 1;

Net中随机函数的算法
Net中随机函数的算法

            retVal = SeedArray[locINext] - SeedArray[locINextp];

Net中随机函数的算法
Net中随机函数的算法

            if (retVal &lt; 0) retVal += MBIG;

Net中随机函数的算法
Net中随机函数的算法

            SeedArray[locINext] = retVal;

Net中随机函数的算法
Net中随机函数的算法

            inext = locINext;

Net中随机函数的算法

            inextp = locINextp;

Net中随机函数的算法
Net中随机函数的算法
Net中随机函数的算法

            return (retVal * (1.0 / MBIG));

Net中随机函数的算法
Net中随机函数的算法
Net中随机函数的算法

        public virtual double NextDouble()

Net中随机函数的算法
Net中随机函数的算法
Net中随机函数的算法

            return Sample();

Net中随机函数的算法
Net中随机函数的算法
Net中随机函数的算法
Net中随机函数的算法

         public virtual int Next(int minValue, int maxValue) ...{

Net中随机函数的算法
Net中随机函数的算法

           if (minValue&gt;maxValue) ...{

Net中随机函数的算法

               throw new ArgumentOutOfRangeException("Argument_MinMaxValue");

Net中随机函数的算法

           }

Net中随机函数的算法
Net中随机函数的算法

           int range = (maxValue-minValue);

Net中随机函数的算法
Net中随机函数的算法

           //This is the case where we flipped around (e.g. MaxValue-MinValue);

Net中随机函数的算法
Net中随机函数的算法

           if (range&lt;0) ...{

Net中随机函数的算法

               long longRange = (long)maxValue-(long)minValue;

Net中随机函数的算法

               return (int)(((long)(Sample()*((double)longRange)))+minValue);

Net中随机函数的算法
Net中随机函数的算法
Net中随机函数的算法

           return ((int)(Sample()*(range)))+minValue;

Net中随机函数的算法

       }

Net中随机函数的算法

    }

使用方法:

static void Main(string[] args)

Net中随机函数的算法
Net中随机函数的算法
Net中随机函数的算法

            MyRnd r = new MyRnd();

Net中随机函数的算法

            Console.WriteLine(r.NextDouble().ToString());

Net中随机函数的算法

            Console.Read();

Net中随机函数的算法

继续阅读