Added randIntExclude

This commit is contained in:
Izwzyzx 2024-10-27 10:35:44 -05:00
parent 0ef7225f13
commit 3f18db888a

View File

@ -3,12 +3,12 @@ using StringTools;
class IzzComLib { 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. * Correctly handles whether parameters are given low-to-high or high-to-low.
* If parameters are equal, returns x. * If parameters are equal, returns x.
* *
* @param X The lower value. * @param x The lower value.
* @param Y The upper value. * @param y The upper value.
*/ */
public static function randInt(x, y) { public static function randInt(x, y) {
if (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<Int>) {
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). * Returns the current time in 24hr notation, with left-padding (HH:MM).
*/ */