1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 00:18:47 +00:00

(#33) Add NextDouble overload with implied lbound

Behaves similar to Random.Next(int) where lower bound defaults to 0
This commit is contained in:
Oliver Booth 2021-03-09 11:51:17 +00:00
parent e05873f383
commit 013e633fce
3 changed files with 53 additions and 1 deletions

View File

@ -87,6 +87,24 @@ namespace X10D {
}
}
/// <summary>
/// Looks up a localized string similar to maxValue must be greater than or equal to 0.
/// </summary>
internal static string MaxValueGreaterThanEqualTo0 {
get {
return ResourceManager.GetString("MaxValueGreaterThanEqualTo0", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to maxValue must be greater than or equal to minValue.
/// </summary>
internal static string MaxValueGreaterThanEqualToMinValue {
get {
return ResourceManager.GetString("MaxValueGreaterThanEqualToMinValue", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The stream does not support reading..
/// </summary>

View File

@ -47,6 +47,9 @@
<data name="StreamDoesNotSupportWriting" xml:space="preserve">
<value>The stream does not support writing.</value>
</data>
<data name="MaxValueGreaterThanEqualTo0" xml:space="preserve">
<value>maxValue must be greater than or equal to 0</value>
</data>
<data name="MaxValueGreaterThanEqualToMinValue" xml:space="preserve">
<value>maxValue must be greater than or equal to minValue</value>
</data>

View File

@ -55,6 +55,37 @@ namespace X10D.RandomExtensions
return random.NextDouble() >= 0.5;
}
/// <summary>
/// Returns a non-negative random double-precision floating point number that is less than the specified maximum.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <param name="maxValue">
/// The exclusive upper bound of the random number returned. This value must be greater than or equal to
/// <paramref name="minValue" />.
/// </param>
/// <returns>
/// A random double-precision floating point number that is greater than or equal to 0, and less than
/// <paramref name="maxValue" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">
/// <paramref name="maxValue" /> is less than <paramref name="minValue" />.
/// </exception>
public static double NextDouble(this Random random, double maxValue)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
if (maxValue < 0)
{
throw new ArgumentOutOfRangeException(ExceptionMessages.MaxValueGreaterThanEqualTo0);
}
return random.NextDouble(0, maxValue);
}
/// <summary>
/// Returns a random double-precision floating point number that is within a specified range.
/// </summary>