From bae8b84d35353b81e36103f79ebc5d8deb31d037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sat, 27 Jan 2024 19:52:42 +0100 Subject: [PATCH] ScriptCore: Added some Random functions --- ScriptCore/Math/Random.cs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 ScriptCore/Math/Random.cs diff --git a/ScriptCore/Math/Random.cs b/ScriptCore/Math/Random.cs new file mode 100644 index 0000000..66bd1ea --- /dev/null +++ b/ScriptCore/Math/Random.cs @@ -0,0 +1,34 @@ +using System; + +namespace GlitchyEngine.Math; + +public static class RandomExtension +{ + /// Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0. + /// A double-precision floating point number that is greater than or equal to 0.0, and less than 1.0. + public static float NextFloat(this Random random) => (float)random.NextDouble(); + + /// Returns a random integer that is within a specified range. + /// + /// The inclusive lower bound of the random number returned. + /// The exclusive upper bound of the random number returned. must be greater than or equal to . + /// A 32-bit signed integer greater than or equal to and less than ; that is, the range of return values includes but not . If equals , is returned. + /// is greater than . + public static int Range(this Random random, int min, int max) => random.Next(min, max); + + /// Returns a random floating-point number that is within a specified range. + /// + /// The inclusive lower bound of the random number returned. + /// The exclusive upper bound of the random number returned. must be greater than or equal to . + /// A 32-bit signed integer greater than or equal to and less than ; that is, the range of return values includes but not . If equals , is returned. + /// is greater than . + public static float Range(this Random random, float min, float max) => (float)(random.NextDouble() * (max - min) + min); + + /// Returns a random floating-point number that is within a specified range. + /// + /// The inclusive lower bound of the random number returned. + /// The exclusive upper bound of the random number returned. must be greater than or equal to . + /// A 32-bit signed integer greater than or equal to and less than ; that is, the range of return values includes but not . If equals , is returned. + /// is greater than . + public static double Range(this Random random, double min, double max) => random.NextDouble() * (max - min) + min; +}