From 3df08760e8297c2ec389696205b419225ca1bc87 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Mon, 25 Apr 2022 21:16:15 +0100 Subject: [PATCH] Replace NextSingle() with NextSingle(float) .NET 6 introduced native NextSingle method, making X10D's implementation redundant --- X10D/src/RandomExtensions/RandomExtensions.cs | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/X10D/src/RandomExtensions/RandomExtensions.cs b/X10D/src/RandomExtensions/RandomExtensions.cs index 3ff23ed..1271c01 100644 --- a/X10D/src/RandomExtensions/RandomExtensions.cs +++ b/X10D/src/RandomExtensions/RandomExtensions.cs @@ -157,19 +157,31 @@ public static class RandomExtensions } /// - /// Returns a random single-precision floating point number between 0 and 1. + /// Returns a non-negative random single-precision floating point number that is less than the specified maximum. /// /// The instance. - /// A random single-precision floating point number between 0 and 1. + /// + /// The exclusive upper bound of the random number returned. This value must be greater than or equal to 0. + /// + /// + /// A random single-precision floating point number that is greater than or equal to 0, and less than + /// . + /// /// is . - public static float NextSingle(this Random random) + /// is less than 0. + public static float NextSingle(this Random random, float maxValue) { if (random is null) { throw new ArgumentNullException(nameof(random)); } - return random.NextSingle(0, 1); + if (maxValue < 0) + { + throw new ArgumentOutOfRangeException(ExceptionMessages.MaxValueGreaterThanEqualTo0); + } + + return random.NextSingle(0, maxValue); } /// @@ -191,7 +203,7 @@ public static class RandomExtensions /// public static float NextSingle(this Random random, float minValue, float maxValue) { - return (float)random.NextDouble(minValue, maxValue); + return MathUtility.Lerp(minValue, maxValue, random.NextSingle()); } ///