这个问题提供了一种在定义范围内生成随机数的方法,您可以按原样将其包装在另一种方法中以暂停随机数秒。
代码可能如下所示:
// Method from the linked answer, copy-pasted here for completeness
// added the relevant fields because the original code is an override
public Int32 GetNewRandomNumber(Int32 minValue, Int32 maxValue)
{
RNGCryptoServiceProvider _rng = new RNGCryptoServiceProvider();
byte[] _uInt32Buffer = new byte[4];
if (minValue > maxValue)
throw new ArgumentOutOfRangeException("minValue");
if (minValue == maxValue) return minValue;
Int64 diff = maxValue - minValue;
while (true)
{
_rng.GetBytes(_uint32Buffer);
UInt32 rand = BitConverter.ToUInt32(_uint32Buffer, 0);
Int64 max = (1 + (Int64)UInt32.MaxValue);
Int64 remainder = max % diff;
if (rand < max - remainder)
{
return (Int32)(minValue + (rand % diff));
}
}
}
public void RandomWait(int minWait, int maxWait)
{
// Thread.Sleep() wants a number of milliseconds to wait
// We're going, as an example, to wait between 8 and 16 seconds
System.Threading.Thread.Sleep(GetNewRandomNumber(8, 16) * 1000);
}
然后,您只需RandomWait()
在需要时拨打电话即可。