zoukankan      html  css  js  c++  java
  • DIY随机数产生类

    自定义随机数产生类

    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;  

            }  

        }  

    }  

  • 相关阅读:
    垂死挣扎-3
    垂死挣扎-2
    垂死挣扎-1
    【互联网考试系列-1】进程与线程
    【iOS基础学习随笔-2】SQLite的使用
    【iOS面试系列-2】多线程中同步、异步和串行、并行之间的逻辑关系(必考,必须掌握)
    docker
    给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
    621. 任务调度器
    204. 计数质数
  • 原文地址:https://www.cnblogs.com/flyinghigher/p/2828136.html
Copyright © 2011-2022 走看看