# 用于上午 7 点至上午 11 点之间的时间的随机时间发生器 [关闭](Random Time Generator for time betweeen 7AM to 11AM [closed])

``````www.random.org/clock-times/
``````

``````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);
// Do something with t...
}
``````

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

``````DateTime start = DateTime.Today.AddHours(7);
Random rnd = new Random();
``````

``````DateTime value = start.AddMinutes(rnd.Next(241));
``````

``````string time = value.ToString("HH:mm");
``````

``````public static DateTime RandomDate(Random generator, DateTime rangeStart, DateTime rangeEnd)
{
TimeSpan span = rangeEnd - rangeStart;

int randomMinutes = generator.Next(0, (int)span.TotalMinutes);
return rangeStart + TimeSpan.FromMinutes(randomMinutes);
}
``````

``````Random rand = new Random();
//Note that Random.Next(int, int) is inclusive lower bound, exclusive upper bound
DateTime myDateTime = new DateTime(2012, 11, 27,
rand.Next(7, 11), rand.Next(0, 60), 0);
``````

``````List<DateTime> randomTimes = new List<DateTime>();
Random r = new Random();
DateTime d = new DateTime(2012, 11, 27, 7, 0, 0);

for (int i = 0; i < 100; i++)
{
TimeSpan t = TimeSpan.FromSeconds(r.Next(0, 14400));
}

randomTimes.Sort();
``````

`randomTimes`列表可与 DateTime 格式一起使用以实现所需的输出格式，例如：

``````Console.WriteLine("HH:mm", randomTimes[0]);
``````

``````var random = new Random();
var startDateTime = new DateTime(2000, 1, 1, 7, 0, 0, 0);
var maxDuration = TimeSpan.FromHours(4);

var values = Enumerable.Range(0, 100)
.Select(x => {
var duration = random.Next(0, (int)maxDuration.TotalMinutes);
})
.ToList();

values = values.Distinct().ToList();

Console.WriteLine("{0} values found. Min: {1}, Max: {2}", values.Count, values.Min(), values.Max());
``````

``````Random r = new Random();

//pick the hour
int h = r.Next(7,12);

//pick the minute
int m = 0;
if(h < 11)
m = r.Next(0,60);

//compose the DateTime
DateTime randomDT = new DateTime(year, month, day, h, m, 0);
``````

``````private static Random rng = new Random();
public IEnumerable<DateTime> RandomDateTimes( DateTime lowerBound , DateTime upperBound , int count )
{
TimeSpan period = upperBound - lowerBound ;

if ( period <= TimeSpan.Zero || period > new TimeSpan(1,0,0,0) ) throw new ArgumentException();
if ( count < 0 ) throw new ArgumentException() ;

int rangeInMinutes = (int) period.TotalMinutes ; // period is 0 through 1440

for ( int i = 0 ; i < count ; ++i )
{
int offset = rng.Next(rangeInMinutes) ;
}
}
public IEnumerable<DateTime> OrderedRandomDateTimes( DateTime lowerbound , DateTime upperBound , int count )
{
yield return RandomDateTimes( lowerbound , upperBound , count ).OrderBy( x = x ) ;
}
``````

``````Random rand = new Random();

// Range can be any number from 100 to 10000
Enumerable.Range(1, 10000).Select(v => TimeSpan.FromMinutes(rand.Next(420, 661)))
``````

``````Random rand = new Random();
int startTime = Convert.ToInt32(TimeSpan.FromHours(7).TotalMinutes);
int endTime = Convert.ToInt32(TimeSpan.FromHours(11).TotalMinutes) + 1;     // To make 11:00 inclusive

Enumerable.Range(1, 10000).Select(v => TimeSpan.FromMinutes(rand.Next(startTime, endTime))).Dump();
``````