Beispiel #1
0
        /// <summary>
        /// Creates a random <see langword="long"/> value within the specified range using a uniform
        /// distribution algorithm.
        /// </summary>
        /// <param name="anon">The anonymous data provider to use.</param>
        /// <param name="minimum">The minimum value.</param>
        /// <param name="maximum">The maximum value.</param>
        /// <returns>A random <see langword="long"/> value.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="anon"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="maximum"/> is less than <paramref name="minimum"/>.</exception>
        public static long AnyInt64(this IAnonymousData anon, long minimum, long maximum)
        {
            Argument.NotNull(anon, nameof(anon));
            Argument.InRange(maximum, minimum, long.MaxValue, nameof(maximum), "The maximum value must be greater than the minimum value.");

            return(anon.AnyInt64(minimum, maximum, Distribution.Uniform));
        }
Beispiel #2
0
        /// <summary>
        /// Creates a <see langword="long"/> value within the specified ranges using the specified factory
        /// and distribution algorithm.
        /// </summary>
        /// <param name="factory">The factory used to create the <see langword="long"/> value.</param>
        /// <param name="distribution">The distribution algorithm to use.</param>
        /// <param name="ranges">The ranges the value must be within.</param>
        /// <returns>A random <see langword="long"/> value that falls within the specified ranges.</returns>
        public static long CreateLongFromRanges(IAnonymousData factory, Distribution distribution, params Range[] ranges)
        {
            Argument.NotNull(factory, nameof(factory));
            Argument.NotNull(ranges, nameof(ranges));

            if (ranges.Length == 0)
            {
                throw new ArgumentException("The ranges must not be empty.", nameof(ranges));
            }

            VerifyRanges(ranges);
            var length = ranges.Sum(r => r.Length);
            var index  = factory.AnyInt64(0, length - 1, distribution);

            for (var i = 0; i < ranges.Length; ++i)
            {
                if (index < ranges[i].Length)
                {
                    var result = ranges[i].Minimum + index;
                    Debug.Assert(result >= ranges[i].Minimum && result <= ranges[i].Maximum, "Invalid range value.");
                    return(result);
                }

                index -= ranges[i].Length;
            }

            throw new InvalidOperationException();
        }
Beispiel #3
0
        /// <summary>
        /// Creates an anonymous <see langword="byte"/> value within the specified range using the specified
        /// distribution algorithm.
        /// </summary>
        /// <param name="anon">The anonymous data provider to use.</param>
        /// <param name="minimum">The minimum value.</param>
        /// <param name="maximum">The maximum value.</param>
        /// <param name="distribution">The distribution algorithm to use.</param>
        /// <returns>A random <see langword="byte"/> value.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="anon"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="maximum"/> is less than <paramref name="minimum"/>.</exception>
        public static byte AnyByte(this IAnonymousData anon, byte minimum, byte maximum, Distribution distribution)
        {
            Argument.NotNull(anon, nameof(anon));
            Argument.InRange(maximum, minimum, double.MaxValue, nameof(maximum), "The maximum value must be greater than the minimum value.");

            return((byte)anon.AnyInt64(minimum, maximum, distribution));
        }
Beispiel #4
0
        /// <summary>
        /// Creates a random <see langword="int"/> value within the specified range using the specified
        /// distribution algorithm.
        /// </summary>
        /// <param name="anon">The anonymous data provider to use.</param>
        /// <param name="minimum">The minimum value.</param>
        /// <param name="maximum">The maximum value.</param>
        /// <param name="distribution">The distribution algorithm to use.</param>
        /// <returns>A random <see langword="int"/> value.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="anon"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="maximum"/> is less than <paramref name="minimum"/>.</exception>
        public static int AnyInt32(this IAnonymousData anon, int minimum, int maximum, Distribution distribution)
        {
            Argument.NotNull(anon, nameof(anon));
            Argument.InRange(maximum, minimum, int.MaxValue, nameof(maximum), "The maximum value must be greater than the minimum value.");

            return((int)anon.AnyInt64(minimum, maximum, distribution));
        }
Beispiel #5
0
        /// <summary>
        /// Creates an anonymous <see langword="char"/> value within the specified range using the specified
        /// distribution algorithm.
        /// </summary>
        /// <param name="anon">The anonymous data provider to use.</param>
        /// <param name="minimum">The minimum value.</param>
        /// <param name="maximum">The maximum value.</param>
        /// <param name="distribution">The distribution algorithm to use.</param>
        /// <returns>A random <see langword="char"/> value.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="anon"/> is <c>null</c>.</exception>
        public static char AnyChar(this IAnonymousData anon, char minimum, char maximum, Distribution distribution)
        {
            Argument.NotNull(anon, nameof(anon));
            Argument.InRange(maximum, minimum, char.MaxValue, nameof(maximum), "The maximum value must be greater than the minimum value.");

            return((char)anon.AnyInt64(minimum, maximum, distribution));
        }
Beispiel #6
0
        /// <summary>
        /// Creates a random <see langword="TimeSpan"/> value within the specified range using the specified
        /// distribution algorithm.
        /// </summary>
        /// <param name="anon">The anonymous data provider to use.</param>
        /// <param name="minimum">The minimum value.</param>
        /// <param name="maximum">The maximum value.</param>
        /// <param name="distribution">The distribution algorithm to use.</param>
        /// <returns>A random <see langword="TimeSpan"/> value.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="anon"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="maximum"/> is less than <paramref name="minimum"/>.</exception>
        public static TimeSpan AnyTimeSpan(this IAnonymousData anon, TimeSpan minimum, TimeSpan maximum, Distribution distribution)
        {
            Argument.NotNull(anon, nameof(anon));
            Argument.InRange(maximum, minimum, TimeSpan.MaxValue, nameof(maximum), "The maximum value must be greater than the minimum value.");

            long ticks = anon.AnyInt64(minimum.Ticks, maximum.Ticks, distribution);

            return(new TimeSpan(ticks));
        }
        /// <summary>
        /// Creates a random <see langword="DateTime"/> value within the specified range using the specified
        /// distribution algorithm.
        /// </summary>
        /// <param name="anon">The anonymous data provider to use.</param>
        /// <param name="minimum">The minimum value.</param>
        /// <param name="maximum">The maximum value.</param>
        /// <param name="distribution">The distribution algorithm to use.</param>
        /// <returns>A random <see langword="DateTime"/> value.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="anon"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="maximum"/> is less than <paramref name="minimum"/>.</exception>
        public static DateTime AnyDateTime(this IAnonymousData anon, DateTime minimum, DateTime maximum, Distribution distribution)
        {
            Argument.NotNull(anon, nameof(anon));
            Argument.InRange(maximum, minimum, DateTime.MaxValue, nameof(maximum), "The maximum value must be greater than the minimum value.");

            var ticks = anon.AnyInt64(minimum.Ticks, maximum.Ticks, distribution);

            return(new DateTime(ticks));
        }
        /// <summary>
        /// Creates a random <see langword="DateTimeOffset"/> value within the specified range using the specified
        /// distribution algorithm.
        /// </summary>
        /// <param name="anon">The anonymous data provider to use.</param>
        /// <param name="minimum">The minimum value.</param>
        /// <param name="maximum">The maximum value.</param>
        /// <param name="distribution">The distribution algorithm to use.</param>
        /// <returns>A random <see langword="DateTimeOffset"/> value.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="anon"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="maximum"/> is less than <paramref name="minimum"/>.</exception>
        public static DateTimeOffset AnyDateTimeOffset(this IAnonymousData anon, DateTimeOffset minimum, DateTimeOffset maximum, Distribution distribution)
        {
            Argument.NotNull(anon, nameof(anon));
            Argument.InRange(maximum, minimum, DateTimeOffset.MaxValue, nameof(maximum), "The maximum value must be greater than the minimum value.");

            var ticks  = anon.AnyInt64(minimum.Ticks, maximum.Ticks, distribution);
            var offset = Offsets[anon.AnyInt32(0, Offsets.Length)];

            return(new DateTimeOffset(ticks, offset));
        }
Beispiel #9
0
        /// <summary>
        /// Creates a random positive <see langword="long"/> value.
        /// </summary>
        /// <param name="anon">The anonymous data provider to use.</param>
        /// <returns>A random positive <see langword="long"/> value.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="anon"/> is <c>null</c>.</exception>
        /// <remarks>
        /// This method may return a zero value, which strictly makes this "any non-negative" from a mathematical
        /// perspective, but the term "positive" is used because this is what many would expect.
        /// </remarks>
        public static long AnyPositiveInt64(this IAnonymousData anon)
        {
            Argument.NotNull(anon, nameof(anon));

            return(anon.AnyInt64(0, long.MaxValue, Distribution.Uniform));
        }
Beispiel #10
0
        /// <summary>
        /// Creates a random <see langword="long"/> value using the specified distribution algorithm.
        /// </summary>
        /// <param name="anon">The anonymous data provider to use.</param>
        /// <param name="distribution">The distribution algorithm to use.</param>
        /// <returns>A random <see langword="long"/> value.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="anon"/> is <c>null</c>.</exception>
        public static long AnyInt64(this IAnonymousData anon, Distribution distribution)
        {
            Argument.NotNull(anon, nameof(anon));

            return(anon.AnyInt64(long.MinValue, long.MaxValue, distribution));
        }
Beispiel #11
0
        /// <summary>
        /// Creates a random positive <see langword="long"/> value.
        /// </summary>
        /// <param name="anon">The anonymous data provider to use.</param>
        /// <returns>A random positive <see langword="long"/> value.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="anon"/> is <c>null</c>.</exception>
        public static long AnyNegativeInt64(this IAnonymousData anon)
        {
            Argument.NotNull(anon, nameof(anon));

            return(anon.AnyInt64(long.MinValue, -1, Distribution.Uniform));
        }