.Net之随机时间生成器
来源:互联网 发布:数据共享怎么用 编辑:程序博客网 时间:2024/05/16 10:36
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DateTime d1 = DateTime.Parse("2010-12-14 00:00:20");
DateTime d2 = DateTime.Parse("2010-12-15 00:00:10");
ArrayList kk = getRandomtimes(d1, d2, 300);
foreach (DateTime d in kk)
{
Response.Write(d.ToString() + "<br/>");
}
}
/// <summary>
/// 获得一个时间段内的随机时间的集合,时间按从小到大排列
/// </summary>
/// <param name="t1">第一个时间</param>
/// <param name="t2">第二个时间</param>
/// <param name="n">要获得的随机数个数</param>
/// <returns>DateTime类型的集合</returns>
public ArrayList getRandomtimes(DateTime t1, DateTime t2, int n)
{
double tt1 = t1.Ticks;
double tt2 = t2.Ticks;
DateTime tbs = tt1 < tt2 ? t1 : t2;//较小时间的刻度数
Int32[] tArr = timeDiff(t1, t2, n);//求间隔秒数
//Int32[] seeds = BubbleSort(tArr); //返回排序后数组
Int32[] seeds = tArr;
ArrayList result = new ArrayList();
DateTime dt = new DateTime();
for (int i = 0; i < seeds.Length; i++)
{
double sd = Convert.ToDouble(seeds[i]);
dt = tbs.AddSeconds(sd);//在基时间上加上时间种子(秒)
result.Add(dt);
}
result.Sort();//排序
return result;
}
/// <summary>
/// 获得一组随机、不重复的时间间隔的秒数差值,最大时间间隔不超过10年
/// </summary>
/// <param name="t1">第一个时间</param>
/// <param name="t2">第二个时间</param>
/// <param name="n">要获得的随机数个数</param>
/// <returns></returns>
public Int32[] timeDiff(DateTime t1, DateTime t2, int n)
{
int i = 0, tmp;
Random rd = new Random();
Int32[] Numbs = new Int32[n];
long ls = Math.Abs(t1.Ticks - t2.Ticks);//刻度数差值的绝对值
System.TimeSpan ts = new TimeSpan(ls);
int tss = Convert.ToInt32(ts.TotalSeconds);//时间跨度的秒数
if (tss > n && tss < 315532801)//跨度秒数大于随机数个数才能确保获得n个不重复随机数;315532800为10年跨度秒数
{
while (i < n)
{
Numbs[i] = rd.Next(tss);
tmp = Numbs[i];
if (i == 0)
{ i = 1; }//从第二个数开始做不重复处理,跳过第一个随机数
else
{
for (int k = 0; k < i; k++)
{
if (tmp != Numbs[k])
tmp = 0;//做标记
}
if (tmp == 0)
{
i++;//当无重复是i才增1,否则Numbs[i]重复赋值
}
}
}
return Numbs;
}
else
{
Response.Write("<script>alert('时间跨度有误!!!')</script>");
Int32[] empt = { };
return empt;//否则返回空数组
}
}
}
- .Net之随机时间生成器
- 随机算法--之等概率生成器
- ASP.net根据时间生成随机文件名
- 随机密码生成器
- 随机生成器示例
- 随机生成器的消耗
- 随机四则运算生成器
- 随机密码生成器
- 几种随机生成器
- 随机文件生成器
- PHP颜色随机生成器
- 随机负样本生成器
- 随机数生成器,随机种子
- 网页随机格言生成器
- 两个.Net代码自动生成器-有时间看看。
- Python学习 随机数据生成器
- 在线随机文本生成器推荐
- 英文名字随机生成器Python库
- 数据库水平切分的实现原理解析
- WinCE控制面板添加应用程序
- jquery ajax send json to action and get response from action
- 传输中指针的作用
- 在web开发中使用dynsrc被McAfee报木马病毒的解决
- .Net之随机时间生成器
- 设计模式汇总(转)
- python 中文问题
- 编写易于理解代码的六种方式
- 通信和通讯有什么区别?
- asp.net页面刷新
- 用javascript控制swf在瀏覽器大小
- Oracle中的一些函数
- j2me中文显示问题