NextDouble实现的随机数

< type="text/JavaScript"> < src="http://a.alimama.cn/inf.js" type="text/javascript">

 NextDouble实现的随机数,实现如下: < type="text/JavaScript"> < src="http://a.alimama.cn/inf.js" type="text/javascript">

view plaincopy to clipboardprint?
//发现我NextDouble的实现很无赖  
using System;  
 
namespace rendom  
{  
    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;  
        }  
    }  
    class AppMain  
    {  
        static int Main()  
        {  
            Rand rand = new Rand();  
            //产生伪随机数  
            long n = rand.Next();  
            Console.WriteLine(n.ToString());  
            //产生100内的伪随机数  
            n = rand.Next(100);  
            Console.WriteLine(n.ToString());  
            //产生20到30之间的伪随机数  
            n = rand.Next(20, 30);  
            Console.WriteLine(n.ToString());  
            //产生0.0到1.0之间的伪随机数  
            b = rand.NextDouble();  
            Console.WriteLine(b.ToString());  
            Console.ReadLine();  
        }  
    }  

//发现我NextDouble的实现很无赖
using System;

namespace rendom
{
    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;
        }
    }
    class AppMain
    {
        static int Main()
        {
            Rand rand = new Rand();
            //产生伪随机数
            long n = rand.Next();
            Console.WriteLine(n.ToString());
            //产生100内的伪随机数
            n = rand.Next(100);
            Console.WriteLine(n.ToString());
            //产生20到30之间的伪随机数
            n = rand.Next(20, 30);
            Console.WriteLine(n.ToString());
            //产生0.0到1.0之间的伪随机数
            b = rand.NextDouble();
            Console.WriteLine(b.ToString());
            Console.ReadLine();
        }
    }
}

< type="text/JavaScript"> < src="http://a.alimama.cn/inf.js" type="text/javascript">

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/gisfarmer/archive/2009/02/03/3860289.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值