From 3f18db888aa150fd85865d47a63c2c98031c8118 Mon Sep 17 00:00:00 2001 From: Izwzyzx <184772711+Izwzyzx@users.noreply.github.com> Date: Sun, 27 Oct 2024 10:35:44 -0500 Subject: [PATCH] Added randIntExclude --- izzcomlib/IzzComLib.hx | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/izzcomlib/IzzComLib.hx b/izzcomlib/IzzComLib.hx index 73be3fc..3397039 100644 --- a/izzcomlib/IzzComLib.hx +++ b/izzcomlib/IzzComLib.hx @@ -3,12 +3,12 @@ using StringTools; class IzzComLib { /** - * Return a random integer between x and y, both inclusive. + * Returns a random integer between x and y, both inclusive. * Correctly handles whether parameters are given low-to-high or high-to-low. * If parameters are equal, returns x. * - * @param X The lower value. - * @param Y The upper value. + * @param x The lower value. + * @param y The upper value. */ public static function randInt(x, y) { if (x < y) { @@ -20,6 +20,31 @@ class IzzComLib { } } + /** + * Returns a random integer between x and y, both inclusive. + * Will not return a result that matches a value in the `exclude` array. + * + * ⚠️ Does not protect against excluding all possible results! The function will loop endlessly! ⚠️ + * + * @param x The lower value. + * @param y The upper value. + * @param exclude Array of values that shouldn't be chosen. + */ + public static function randIntExclude(x, y, exclude:Array) { + if (x == y) { + return x; // Very minimal mistake protection + } else { + var result:Int = 0; + + while (true) { + result = randInt(x, y); + if (!exclude.contains(result)) {break;} + } + + return result; + } + } + /** * Returns the current time in 24hr notation, with left-padding (HH:MM). */