熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

c#DIY隨機數類

2022-06-13   來源: .NET編程 

  在類庫橫行的今天請支持DIY 直接看代碼(有注釋)

  view plaincopy to clipboardprint?
using System;

  namespace MyRandom
{
    public class Rand
    {
        private long seed;  //隨機數種子

  //用系統時間作為隨機種子
        public Rand()
        {
            string str = DateTimeNowDayToString();
            str += DateTimeNowHourToString();
            str += DateTimeNowMinuteToString();
            str += DateTimeNowSecondToString();
            str += DateTimeNowMillisecondToString();
            thisseed = longParse(str);
        }
        //用戶自定義隨機種子
        public Rand(long Value)
        {
            thisseed = Value;
        }

  //產生非負偽隨機數
        public long Next()
        {
            long l = thisseed;
            l = l * l;
            string strTime = lToString();
            int w = strTimeLength / ;
            string str = ;
            for (int i = w; i < strTimeLength w; i++)
                str += strTime[i];
            l = longParse(str);
            return l;
        }
        //產生上限為Value的偽隨機數
        public long Next(long Value)
        {
            if (Value < )
                return ;
            long l = thisseed;
            l = l * l;
            string strTime = lToString();
            int w = strTimeLength / ;
            string str = ;
            for (int i = w; i < strTimeLength w; i++)
                str += strTime[i];
            l = longParse(str);
            return l % Value;
        }
        //產生下限為minValue上限為maxValue的偽隨機數
        public long Next(long minValue long maxValue)
        {
            if (minValue < || maxValue < || minValue > maxValue)
                return ;
            long l = thisseed;
            l = l * l;
            string strTime = lToString();
            int w = strTimeLength / ;
            string str = ;
            for (int i = w; i < strTimeLength w; i++)
                str += strTime[i];
            l = longParse(str);
            return l % (maxValue minValue) + minValue;
        }
        //偽隨機數填充指定的比特數組
        public void NextBytes(byte[] buffer)
        {
            long Value = ;
            Value = thisNext() % ;
            for (int i = ; i < bufferLength; i++)
            {
                Value = Value * Value;
                string strTime = ValueToString();
                int w = strTimeLength / ;
                string str = ;
                for (int j = w; j < strTimeLength w; j++)
                    str += strTime[j];
                Value = longParse(str);
                Value = Value % ;
                buffer[i] = (byte)Value;
            }
        }
        //產生的偽隨機數
        public double NextDouble()
        {
            long l = thisNext( );
            if (l == )
                return ;
            l = l * l;
            string strTime = lToString();
            int w = strTimeLength / ;
            string str = ;
            for (int i = w; i < strTimeLength w; i++)
                str += strTime[i];
            double d = doubleParse(str);
            return d;
        }
    }
}


From:http://tw.wingwit.com/Article/program/net/201311/13253.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.