// Copyright (C) Stichting Deltares 2016. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using Core.Common.Base.Data; namespace Core.Common.TestUtil { /// /// Extension methods for . /// public static class RandomExtensions { /// /// Generates a new pseudo-random number between and . /// /// A pseudo-random number generator. /// The lower limit of the new random number. /// The upper limit of the new random number. /// A new random number. /// Thrown when is null. /// Thrown when is larger than . /// Thrown when the generated value is not finite. public static double GetFromRange(this Random random, double lowerLimit, double upperLimit) { if (random == null) { throw new ArgumentNullException("random"); } if (lowerLimit > upperLimit) { throw new ArgumentException("lowerLimit is larger than upperLimit"); } double difference = upperLimit - lowerLimit; double randomValue = lowerLimit + random.NextDouble()*difference; if (double.IsInfinity(randomValue) || double.IsNaN(randomValue)) { string message = string.Format("Creating a new random value with lower limit {0} " + "and upper limit {1} did not result in a finite value.", lowerLimit, upperLimit); throw new NotFiniteNumberException(message, randomValue); } return randomValue; } /// /// Returns a random boolean value. /// /// A pseudo-random number generator. /// A new random boolean value. /// Thrown when is null. public static bool NextBoolean(this Random random) { if (random == null) { throw new ArgumentNullException("random"); } return Convert.ToBoolean(random.Next(0, 2)); } /// /// Returns a random value. /// /// A pseudo-random number generator. /// A new random . /// Thrown when is null. public static RoundedDouble NextRoundedDouble(this Random random) { if (random == null) { throw new ArgumentNullException("random"); } return (RoundedDouble)random.NextDouble(); } /// /// Returns a random value of . /// /// The to use. /// A pseudo-random number generator. /// >A new random value of type . /// Thrown when is null. /// Thrown when is not an . public static TEnum NextEnumValue(this Random random) { if (random == null) { throw new ArgumentNullException("random"); } var enumValues = (TEnum[]) Enum.GetValues(typeof(TEnum)); return enumValues[random.Next(enumValues.Length)]; } } }