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). */