国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

c#DIY隨機數類

2019-11-17 04:20:16
字體:
來源:轉載
供稿:網友

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

view plaincopy to clipboardPRint?
using System;  
 
namespace MyRandom  
{  
    public class Rand  
    {  
        private long seed;  //隨機數種子   
 
        //用系統時間作為隨機種子   
        public Rand()  
        {  
            string str = DateTime.Now.Day.ToString();  
            str += DateTime.Now.Hour.ToString();  
            str += DateTime.Now.Minute.ToString();  
            str += DateTime.Now.Second.ToString();  
            str += DateTime.Now.Millisecond.ToString();  
            this.seed = long.Parse(str);  
        }  
        //用戶自定義隨機種子   
        public Rand(long Value)  
        {  
            this.seed = Value;  
        }  
 
        //產生非負偽隨機數   
        public long Next()  
        {  
            long l = this.seed;  
            l = l * l;  
            string strTime = l.ToString();  
            int w = strTime.Length / 3;  
            string str = "";  
            for (int i = w; i < strTime.Length - w; i++)  
                str += strTime[i];  
            l = long.Parse(str);  
            return l;  
        }  
        //產生上限為Value的偽隨機數   
        public long Next(long Value)  
        {  
            if (Value < 0)  
                return -1;  
            long l = this.seed;  
            l = l * l;  
            string strTime = l.ToString();  
            int w = strTime.Length / 3;  
            string str = "";  
            for (int i = w; i < strTime.Length - w; i++)  
                str += strTime[i];  
            l = long.Parse(str);  
            return l % Value;  
        }  
        //產生下限為minValue上限為maxValue的偽隨機數   
        public long Next(long minValue, long maxValue)  
        {  
            if (minValue < 0 || maxValue < 0 || minValue > maxValue)  
                return -1;  
            long l = this.seed;  
            l = l * l;  
            string strTime = l.ToString();  
            int w = strTime.Length / 3;  
            string str = "";  
            for (int i = w; i < strTime.Length - w; i++)  
                str += strTime[i];  
            l = long.Parse(str);  
            return l % (maxValue - minValue) + minValue;  
        }  
        //偽隨機數填充指定的比特數組   
        public void NextBytes(byte[] buffer)  
        {  
            long Value = 0;  
            Value = this.Next() % 255;  
            for (int i = 0; i < buffer.Length; i++)  
            {  
                Value = Value * Value;  
                string strTime = Value.ToString();  
                int w = strTime.Length / 3;  
                string str = "";  
                for (int j = w; j < strTime.Length - w; j++)  
                    str += strTime[j];  
                Value = long.Parse(str);  
                Value = Value % 255;  
                buffer[i] = (byte)Value;  
            }  
        }  
        //產生0.0到1.0的偽隨機數   
        public double NextDouble()  
        {  
            long l = this.Next(0, 100000);  
            if (l == 100000)  
                return 1.0;  
            l = l * l;  
            string strTime = l.ToString();  
            int w = strTime.Length / 3;  
            string str = "0.";  
            for (int i = w; i < strTime.Length - w; i++)  
                str += strTime[i];  
            double d = double.Parse(str);  
            return d;  
        }  
    }  
}
http://blog.csdn.net/gisfarmer/archive/2009/02/03/3860282.aspx


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南澳县| 香港 | 钦州市| 嘉定区| 兰溪市| 浦县| 安义县| 镇赉县| 保山市| 柳江县| 定襄县| 江口县| 大安市| 广宁县| 且末县| 余庆县| 蓬溪县| 桦川县| 涪陵区| 米林县| 皋兰县| 拜泉县| 文成县| 乐安县| 庐江县| 射阳县| 乐陵市| 突泉县| 工布江达县| 阿瓦提县| 兰坪| 新蔡县| 嘉义市| 双牌县| 金坛市| 富阳市| 佛山市| 石台县| 江城| 新泰市| 章丘市|