Esempio n. 1
0
        public static TimeSpan NextTimeSpan(this SafeRandom random, TimeSpan timeSpan)
        {
            if (timeSpan <= TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(timeSpan), timeSpan, "SafeRandom.NextTimeSpan timeSpan must be a positive number.");
            }

            var ticksDouble = timeSpan.Ticks * random.NextDouble();
            var ticks       = checked ((long)ticksDouble);

            return(TimeSpan.FromTicks(ticks));
        }
Esempio n. 2
0
        public static TimeSpan NextTimeSpan(this SafeRandom random, TimeSpan minValue, TimeSpan maxValue)
        {
            if (minValue <= TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(minValue), minValue, "SafeRandom.NextTimeSpan minValue must be a positive number.");
            }
            if (minValue >= maxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(minValue), minValue, "SafeRandom.NextTimeSpan minValue must be less than maxValue.");
            }

            var span = maxValue - minValue;

            return(minValue + random.NextTimeSpan(span));
        }