[英]How to generate a random number based on the time of the day?
我在 Azure 物聯網集線器中模擬物聯網設備(噪聲傳感器),下面的代碼工作得很好。 但是我想模擬一些更接近現實的東西,我可以在不同的時間使用不同的分貝范圍。
像這樣的東西:
if 00.00- 7.00AM - Randum number between (10-20)
if 7am-9AM - Random number between (20-40)
if 11.30-1.30pm Random number between 60-80
我不想創建大量 IF、Elses,因為我想要更簡潔的代碼。
我應該如何以結構化的方式做到這一點?
我的代碼如下:(僅相關方法)
private static async Task SendDeviceToCloudMessagesAsync(CancellationToken ct)
{
// Initial telemetry values
int minNoise = 20;
int maxNoise = 90;
var rand = new Random();
while (!ct.IsCancellationRequested)
{
double noiseDecibels = rand.Next(minNoise, maxNoise);
// Create JSON message
string messageBody = JsonSerializer.Serialize(
new
{
eui= "58A0CB0000101DB6",
DecibelValue = noiseDecibels
});
using var message = new Message(Encoding.ASCII.GetBytes(messageBody))
{
ContentType = "application/json",
ContentEncoding = "utf-8",
};
// Add a custom application property to the message.
// An IoT hub can filter on these properties without
// access to the message body.
message.Properties.Add("noiseAlert", (noiseDecibels > 70) ? "true" : "false");
// Send the telemetry message
await s_deviceClient.SendEventAsync(message);
Console.WriteLine($"{DateTime.Now} > Sending message: {messageBody}");
await Task.Delay(60000);
}
}
如果您有這樣的特定條件,唯一的方法是手動檢查,其中哪些適用。
private Random randomGenerator = new Random();
function decibel(DateTime d) {
int randMin = 0, randMax = 0; //the interval for the random value
var t = d.TimeOfDay;
if (t.TotalHours < 7) //0:00 - 6:59:59
{
randMin = 10; randMax = 20;
}
else if (d.TotalHours < 9) //7:00 - 8:59:59
{
randMin = 20; randMax = 40;
}
else if (d.TotalHours >= 11.5 && d.TotalHours < 13.5) //11:30 - 13:29:59
{
randMin = 60; randMax = 80;
}
...
// returns a rand with: randMin <= rand < randMax
return randomGenerator.Next(randMin, randMax);
}
我在數據結構模式中處理這個問題(就像你問的那樣),所以我會創建一個繼承自 Dictionary 的 RangeHourDictionary class。
Add
方法將添加鍵表示開始時間和結束時間的范圍。 這些值將是一個大小為 2 的 int 數組,其中第一個值表示開始范圍,第二個值表示范圍的結束。
另一個 function 將是GetRandomRange
,它將獲取當前時間並返回值(再次表示隨機范圍的開始和結束)
public class RangeDictionary : Dictionary<Range, int[]>
{
public void Add(TimeSpan from, TimeSpan to, int[] randomValues)
{
Add(new Range(from, to), randomValues);
}
public int[] GetRandomRange(TimeSpan now)
{
try
{
return this.First(x => x.Key.From < now && x.Key.To > now).Value;
}
catch
{
return null;
}
}
}
public struct Range
{
public Range(TimeSpan from, TimeSpan to) : this()
{
From = from;
To = to;
}
public TimeSpan From { get; }
public TimeSpan To { get; }
}
//initialize
var lookup = new RangeDictionary();
lookup.Add(new TimeSpan(07, 0, 0), new TimeSpan(09, 0, 0), new int[2] { 10, 20 });
lookup.Add(new TimeSpan(09, 30, 0), new TimeSpan(11, 0, 0), new int[2] { 40, 50 });
lookup.Add(new TimeSpan(11, 0, 0), new TimeSpan(13, 0, 0), new int[2] { 60, 80 });
// call GetRandomRange
var res = lookup.GetRandomRange(DateTime.Now.TimeOfDay);
if (res != null){
Random random = new Random();
var randomValue = random.Next(res[0], res[1]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.