-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomValuesGenerator.cs
60 lines (40 loc) · 1.91 KB
/
RandomValuesGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
namespace YamatoDaiwa.CSharpExtensions;
public abstract class RandomValuesGenerator
{
public static bool GetRandomBoolean()
{
return new Random().Next() > (Int32.MaxValue / 2);
}
public static byte GetRandomByte(byte minimalValue = Byte.MinValue, byte maximalValue = Byte.MaxValue)
{
return Convert.ToByte(new Random().Next(minValue: minimalValue, maxValue: maximalValue));
}
public static ushort GetRandomUShort(ushort minimalValue = ushort.MinValue, ushort maximalValue = ushort.MaxValue)
{
return Convert.ToUInt16(new Random().Next(minValue: minimalValue, maxValue: maximalValue));
}
public static TArrayElement GetRandomArrayElement<TArrayElement>(TArrayElement[] targetArray)
{
if (targetArray == null || targetArray.Length == 0)
{
throw new ArgumentException("Array must not be null or empty.");
}
return targetArray[new Random().Next(0, targetArray.Length)];
}
public static DateOnly GetRandomDate(DateOnly earliestDate, DateOnly latestDate)
{
DateTime earliestDateTime = earliestDate.ToDateTime(new TimeOnly(0, 0, 0));
DateTime latestDateTime = latestDate.ToDateTime(new TimeOnly(23, 59, 59));
TimeSpan randomTimeSpan = TimeSpan.FromTicks((long)(new Random().NextDouble() * (latestDateTime - earliestDateTime).Ticks));
DateTime randomDateTime = earliestDateTime + randomTimeSpan;
return DateOnly.FromDateTime(randomDateTime.Date);
}
public static DateTime GetRandomDateTime(DateOnly earliestDate, DateOnly latestDate)
{
DateTime earliestDateTime = earliestDate.ToDateTime(new TimeOnly(0, 0, 0));
DateTime latestDateTime = latestDate.ToDateTime(new TimeOnly(23, 59, 59));
TimeSpan randomTimeSpan = TimeSpan.FromTicks((long)(new Random().NextDouble() * (latestDateTime - earliestDateTime).Ticks));
DateTime randomDateTime = earliestDateTime + randomTimeSpan;
return randomDateTime;
}
}