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

2008-02-23 05:43:31来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

原理其实很简单,取出两个时间差的秒数,再在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


标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: 根据html页面模板动态生成html页面(c#类)_c#应用

下一篇: c#箴言:用静态构造函数初始化静态成员_c#应用