public static TimeSpan NextTimeSpan(this SafeRandom random, TimeSpan timeSpan) { if (timeSpan <= TimeSpan.Zero) { throw new ArgumentOutOfRangeException("timeSpan", timeSpan, "SafeRandom.NextTimeSpan timeSpan must be a positive number."); } double ticksD = ((double)timeSpan.Ticks) * random.NextDouble(); long ticks = checked ((long)ticksD); return(TimeSpan.FromTicks(ticks)); }
public static TimeSpan NextTimeSpan(this SafeRandom random, TimeSpan minValue, TimeSpan maxValue) { if (minValue <= TimeSpan.Zero) { throw new ArgumentOutOfRangeException("minValue", minValue, "SafeRandom.NextTimeSpan minValue must be a positive number."); } if (minValue >= maxValue) { throw new ArgumentOutOfRangeException("minValue", minValue, "SafeRandom.NextTimeSpan minValue must be greater than maxValue."); } var span = maxValue - minValue; return(minValue + random.NextTimeSpan(span)); }
public ExponentialBackoff(TimeSpan minDelay, TimeSpan maxDelay, TimeSpan step) { if (minDelay <= TimeSpan.Zero) { throw new ArgumentOutOfRangeException("minDelay", minDelay, "ExponentialBackoff min delay must be a positive number."); } if (maxDelay <= TimeSpan.Zero) { throw new ArgumentOutOfRangeException("maxDelay", maxDelay, "ExponentialBackoff max delay must be a positive number."); } if (step <= TimeSpan.Zero) { throw new ArgumentOutOfRangeException("step", step, "ExponentialBackoff step must be a positive number."); } if (minDelay >= maxDelay) { throw new ArgumentOutOfRangeException("minDelay", minDelay, "ExponentialBackoff min delay must be greater than max delay."); } this.minDelay = minDelay; this.maxDelay = maxDelay; this.step = step; this.random = new SafeRandom(); }