// SleepRandomTime()
//
// This sleeps a random amount of time defined by an exponential probability distribution. The mean time, in Seconds is given
// in 'mean_time'.
//
// This is the back-off strategy used by Ethernet. This will
// quantize in fiftieths of seconds, so don't call this with a too
// small a number. This returns immediately if we are backtesting
// and does not sleep.
//
//=============================================================================
void SleepRandomTime(double mean_time, double max_time)
{
if (IsTesting())
return; // return immediately if backtesting.
double tenths = MathCeil(mean_time / 0.1);// 40
if (tenths <= 0)
return;
int maxtenths = MathRound(max_time / 0.1);// 250
double p = 1.0 - 1.0 / tenths;// 0.975
// Always sleep at least one-hundredth second
Sleep(20);
// Now loop through and sleep 1/10th second a max of
// (10 * mean_time) times. But break out "randomly".
for (int i = 0; i < maxtenths; i++)
{
if (MathRand() > p*32768)
break;
// MathRand() returns in 0..32767
Sleep(20);
}
}
微信公众号:天泓评测
发表评论