原理其实非常简单,取出两个时间差的秒数,再在0到该秒数之间随机获取一个整数,将其做为秒添加到较小的时间上,可以说实现上并没什么技术难点,可以在数据类型的边界条件上却需要格外的注意,比如将大于 System.Int32.MaxValue 或小于 System.Int32.MinValue 的值转成 int 时,如果直接在变量前加上类型名转换((int)d),不会有有异常产生,但得到的值却是 System.Int32.MinValue,还有就是对于Math.Abs 方法,当参数 value 等于 MinValue 将会引发 System.OverflowException 异常。
代码如下:
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/None.gif)
using System;
![](/Images/OutliningIndicators/None.gif)
namespace Yyw
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public class DateTimeHelper
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Random random = new Random();
return GetRandomTime(time1, time2, random);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DateTime minTime = new DateTime();
DateTime maxTime = new DateTime();
![](/Images/OutliningIndicators/InBlock.gif)
System.TimeSpan ts = new System.TimeSpan(time1.Ticks - time2.Ticks);
![](/Images/OutliningIndicators/InBlock.gif)
// 获取两个时间相隔的秒数
double dTotalSecontds = ts.TotalSeconds;
int iTotalSecontds = 0;
![](/Images/OutliningIndicators/InBlock.gif)
if (dTotalSecontds > System.Int32.MaxValue)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iTotalSecontds = System.Int32.MaxValue;
}
else if (dTotalSecontds < System.Int32.MinValue)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iTotalSecontds = System.Int32.MinValue;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iTotalSecontds = (int)dTotalSecontds;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
if (iTotalSecontds > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
minTime = time2;
maxTime = time1;
}
else if (iTotalSecontds < 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
minTime = time1;
maxTime = time2;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return time1;
}
![](/Images/OutliningIndicators/InBlock.gif)
int maxValue = iTotalSecontds;
![](/Images/OutliningIndicators/InBlock.gif)
if (iTotalSecontds <= System.Int32.MinValue)
maxValue = System.Int32.MinValue + 1;
![](/Images/OutliningIndicators/InBlock.gif)
int i = random.Next(System.Math.Abs(maxValue));
![](/Images/OutliningIndicators/InBlock.gif)
return minTime.AddSeconds(i);
}
}
}
测试代码:
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using NUnit.Framework;
![](/Images/OutliningIndicators/None.gif)
namespace Yyw
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
[TestFixture]
public class DateTimeHelperFixture
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 边界测试
/// </summary>
[Test]
public void TestGetRandomTime()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DateTime minTime = DateTime.MinValue;
DateTime maxTime = DateTime.MaxValue;
Random random = new Random();
![](/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < 10000; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DateTime randomTime = DateTimeHelper.GetRandomTime(maxTime, minTime, random);
CheckResult(minTime, maxTime, randomTime);
}
![](/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < 10000; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DateTime randomTime = DateTimeHelper.GetRandomTime(minTime, maxTime, random);
CheckResult(minTime, maxTime, randomTime);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
private void CheckResult(DateTime minTime, DateTime maxTime, DateTime randomTime)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
}
}
}