From 2e8a8244a39ffd2cb0f7c0219d9060cb99e07da1 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 7 Jan 2023 20:01:22 +0100 Subject: [PATCH] Added RandomExtensions.NextSingle with minimum and maximum values --- CHANGELOG.md | 1 + MLEM/Extensions/RandomExtensions.cs | 32 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5d48cc..b0baded 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ Additions - Added SingleRandom and SeedSource - Added TokenizedString.GetArea - Added InputHandler.WasPressedForLess and related methods as well as InputHandler.IsPressedIgnoreRepeats +- Added RandomExtensions.NextSingle with minimum and maximum values - **Added the ability to find paths to one of multiple goals using AStar** Improvements diff --git a/MLEM/Extensions/RandomExtensions.cs b/MLEM/Extensions/RandomExtensions.cs index 0741046..0a5871d 100644 --- a/MLEM/Extensions/RandomExtensions.cs +++ b/MLEM/Extensions/RandomExtensions.cs @@ -55,5 +55,37 @@ namespace MLEM.Extensions { throw new IndexOutOfRangeException(); } + /// + /// Returns a random floating-point number that is greater than or equal to 0, and less than . + /// + /// The random. + /// The (exclusive) maximum value. + /// A single-precision floating point number that is greater than or equal to 0, and less than . + public static float NextSingle(this Random random, float maxValue) { + return maxValue * random.NextSingle(); + } + + /// + /// Returns a random floating-point number that is greater than or equal to , and less than . + /// + /// The random. + /// The (inclusive) minimum value. + /// The (exclusive) maximum value. + /// A single-precision floating point number that is greater than or equal to , and less than . + public static float NextSingle(this Random random, float minValue, float maxValue) { + return (maxValue - minValue) * random.NextSingle() + minValue; + } + +#if !NET6_0_OR_GREATER + /// + /// Returns a random floating-point number that is greater than or equal to 0, and less than 1. + /// + /// The random. + /// A single-precision floating point number that is greater than or equal to 0, and less than 1. + public static float NextSingle(this Random random) { + return (float) random.NextDouble(); + } +#endif + } }