7AM 到 11AM 时间的随机时间发生器 [英] Random Time Generator for time betweeen 7AM to 11AM

查看:29
本文介绍了7AM 到 11AM 时间的随机时间发生器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个测试文件,我需要在上午 7 点到上午 11 点之间用随机时间填充它.只要不完全相同,重复输入就可以了

I am creating a test file and I need to fill it with random times between 7AM to 11AM. Repeating entries are OK as long as they aren't all the same

我也只对 HH:MM(没有秒)感兴趣

I'm also only interested in HH:MM (no seconds)

我不知道从哪里开始.发帖前用谷歌搜索了一下,发现一个有趣的搜索结果

I don't know where to start. I did Google before posting and I found an interesting search result

www.random.org/clock-times/

唯一的问题是所有随机"生成的时间都是按顺序排列的.我可以把它打乱一次,但我需要生成 100 到 10,000 个条目.

Only issue is that all times "randomly" generated are in sequential order. I can put it out of sequence once but I need to generate 100 to 10,000 entries.

我希望创建一个 WinForm C# 应用程序来帮助我做到这一点.

I am hoping to create a WinForm C# app that will help me do this.

推荐答案

计算开始和停止时间之间的分钟数,然后生成一个介于 0 和最大分钟数之间的随机数:

Calculate the number of minutes between your start and stop times then generate a random number between 0 and the maximum number of minutes:

Random random = new Random();
TimeSpan start = TimeSpan.FromHours(7);
TimeSpan end = TimeSpan.FromHours(11);
int maxMinutes = (int)((end - start).TotalMinutes);

for (int i = 0; i < 100; ++i) {
   int minutes = random.Next(maxMinutes);
   TimeSpan t = start.Add(TimeSpan.FromMinutes(minutes));
   // Do something with t...
}

注意事项:

  • 你应该只创建一个随机对象,否则你会连续得到很多重复的对象.
  • 包含开始时间,但不包含结束时间.如果您还想包括结束时间,请将 maxMinutes 加 1.

这篇关于7AM 到 11AM 时间的随机时间发生器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆