C# 中实现随机时间的获取


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

代码如下:
ContractedBlock.gif ExpandedBlockStart.gif
None.gif
None.gif
using System;
None.gif
None.gif
namespace Yyw
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class DateTimeHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取随机时间
InBlock.gif        
/// <remarks>
InBlock.gif        
/// 由于Random 以当前系统时间做为种值,所以当快速运行多次该方法所得到的结果可能相同,
InBlock.gif        
/// 这时,你应该在外部初始化 Random 实例并调用 GetRandomTime(DateTime time1, DateTime time2, Random random)
InBlock.gif        
/// </remarks>
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="time1"></param>
InBlock.gif        
/// <param name="time2"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static DateTime GetRandomTime(DateTime time1, DateTime time2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Random random 
= new Random();
InBlock.gif            
return GetRandomTime(time1, time2, random);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取随机时间
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="time1"></param>
InBlock.gif        
/// <param name="time2"></param>
InBlock.gif        
/// <param name="random"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static DateTime GetRandomTime(DateTime time1, DateTime time2, Random random)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DateTime minTime 
= new DateTime();
InBlock.gif            DateTime maxTime 
= new DateTime();
InBlock.gif
InBlock.gif            System.TimeSpan ts 
= new System.TimeSpan(time1.Ticks - time2.Ticks);
InBlock.gif
InBlock.gif            
// 获取两个时间相隔的秒数
InBlock.gif
            double dTotalSecontds = ts.TotalSeconds;
InBlock.gif            
int iTotalSecontds = 0;
InBlock.gif
InBlock.gif            
if (dTotalSecontds > System.Int32.MaxValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                iTotalSecontds 
= System.Int32.MaxValue;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (dTotalSecontds < System.Int32.MinValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                iTotalSecontds 
= System.Int32.MinValue;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                iTotalSecontds 
= (int)dTotalSecontds;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
InBlock.gif            
if (iTotalSecontds > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                minTime 
= time2;
InBlock.gif                maxTime 
= time1;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (iTotalSecontds < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                minTime 
= time1;
InBlock.gif                maxTime 
= time2;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return time1;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
int maxValue = iTotalSecontds;
InBlock.gif
InBlock.gif            
if (iTotalSecontds <= System.Int32.MinValue)
InBlock.gif                maxValue 
= System.Int32.MinValue + 1;
InBlock.gif
InBlock.gif            
int i = random.Next(System.Math.Abs(maxValue));
InBlock.gif
InBlock.gif            
return minTime.AddSeconds(i);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

测试代码:
ContractedBlock.gif ExpandedBlockStart.gif
None.gifusing System;
None.gif
using NUnit.Framework;
None.gif
None.gif
namespace Yyw
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    [TestFixture]
InBlock.gif    
public class DateTimeHelperFixture
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 边界测试
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Test]
InBlock.gif        
public void TestGetRandomTime()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DateTime minTime 
= DateTime.MinValue;
InBlock.gif            DateTime maxTime 
= DateTime.MaxValue;
InBlock.gif            Random random 
= new Random();
InBlock.gif
InBlock.gif            
for (int i = 0; i < 10000; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DateTime randomTime 
= DateTimeHelper.GetRandomTime(maxTime, minTime, random);
InBlock.gif                CheckResult(minTime, maxTime, randomTime);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
for (int i = 0; i < 10000; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DateTime randomTime 
= DateTimeHelper.GetRandomTime(minTime, maxTime, random);
InBlock.gif                CheckResult(minTime, maxTime, randomTime);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void CheckResult(DateTime minTime, DateTime maxTime, DateTime randomTime)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.TimeSpan ts1 
= new System.TimeSpan(randomTime.Ticks - minTime.Ticks);
InBlock.gif            System.TimeSpan ts2 
= new System.TimeSpan(randomTime.Ticks - maxTime.Ticks);
InBlock.gif            Assert.IsTrue(ts1.Seconds 
>= 0 && ts2.Seconds <= 0);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值