欢迎光临
我们一直在努力

c#中实现随机时间的获取_c#应用

建站超值云服务器,限时71元/月

    原理其实非常简单,取出两个时间差的秒数,再在0到该秒数之间随机获取一个整数,将其做为秒添加到较小的时间上,可以说实现上并没什么技术难点,可以在数据类型的边界条件上却需要格外的注意,比如将大于 System.Int32.MaxValue 或小于 System.Int32.MinValue 的值转成 int 时,如果直接在变量前加上类型名转换((int)d),不会有有异常产生,但得到的值却是 System.Int32.MinValue,还有就是对于Math.Abs 方法,当参数 value 等于 MinValue 将会引发 System.OverflowException 异常。


代码如下:



using System;


namespace Yyw
{
    public class DateTimeHelper
    {
        /**//// <summary>
        /// 获取随机时间
        /// <remarks>
        /// 由于Random 以当前系统时间做为种值,所以当快速运行多次该方法所得到的结果可能相同,
        /// 这时,你应该在外部初始化 Random 实例并调用 GetRandomTime(DateTime time1, DateTime time2, Random random)
        /// </remarks>
        /// </summary>
        /// <param name=”time1″></param>
        /// <param name=”time2″></param>
        /// <returns></returns>
        public static DateTime GetRandomTime(DateTime time1, DateTime time2)
        {
            Random random = new Random();
            return GetRandomTime(time1, time2, random);
        }


        /**//// <summary>
        /// 获取随机时间
        /// </summary>
        /// <param name=”time1″></param>
        /// <param name=”time2″></param>
        /// <param name=”random”></param>
        /// <returns></returns>
        public static DateTime GetRandomTime(DateTime time1, DateTime time2, Random random)
        {
            DateTime minTime = new DateTime();
            DateTime maxTime = new DateTime();


            System.TimeSpan ts = new System.TimeSpan(time1.Ticks – time2.Ticks);


            // 获取两个时间相隔的秒数
            double dTotalSecontds = ts.TotalSeconds;
            int iTotalSecontds = 0;


            if (dTotalSecontds > System.Int32.MaxValue)
            {
                iTotalSecontds = System.Int32.MaxValue;
            }
            else if (dTotalSecontds < System.Int32.MinValue)
            {
                iTotalSecontds = System.Int32.MinValue;
            }
            else
            {
                iTotalSecontds = (int)dTotalSecontds;
            }



            if (iTotalSecontds > 0)
            {
                minTime = time2;
                maxTime = time1;
            }
            else if (iTotalSecontds < 0)
            {
                minTime = time1;
                maxTime = time2;
            }
            else
            {
                return time1;
            }


            int maxValue = iTotalSecontds;


            if (iTotalSecontds <= System.Int32.MinValue)
                maxValue = System.Int32.MinValue + 1;


            int i = random.Next(System.Math.Abs(maxValue));


            return minTime.AddSeconds(i);
        }
    }
}
测试代码:


using System;
using NUnit.Framework;


namespace Yyw
{
    [TestFixture]
    public class DateTimeHelperFixture
    {
        /**//// <summary>
        /// 边界测试
        /// </summary>
        [Test]
        public void TestGetRandomTime()
        {
            DateTime minTime = DateTime.MinValue;
            DateTime maxTime = DateTime.MaxValue;
            Random random = new Random();


            for (int i = 0; i < 10000; i++)
            {
                DateTime randomTime = DateTimeHelper.GetRandomTime(maxTime, minTime, random);
                CheckResult(minTime, maxTime, randomTime);
            }


            for (int i = 0; i < 10000; i++)
            {
                DateTime randomTime = DateTimeHelper.GetRandomTime(minTime, maxTime, random);
                CheckResult(minTime, maxTime, randomTime);
            }
        }


        private void CheckResult(DateTime minTime, DateTime maxTime, DateTime randomTime)
        {
            System.TimeSpan ts1 = new System.TimeSpan(randomTime.Ticks – minTime.Ticks);
            System.TimeSpan ts2 = new System.TimeSpan(randomTime.Ticks – maxTime.Ticks);
            Assert.IsTrue(ts1.Seconds >= 0 && ts2.Seconds <= 0);
        }
    }
}
http://www.cnblogs.com/yyw84/archive/2006/09/07/497800.html

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » c#中实现随机时间的获取_c#应用
分享到: 更多 (0)