Skip to content

Commit

Permalink
feat(utils): validate input value before calculating chance
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Feb 12, 2022
1 parent f5b716e commit f5c4d18
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,33 @@ public static <T> T pickOneFromList(List<T> list) {
/*================================================================================================================*/
// #region Chance

private static boolean percentageChance(int random, int chance) {
Validate.isInCloseInterval(chance, 0, 100);
return random < chance;
}

public static boolean percentageChance(int chance) {
return percentageChance(nextInt(0, 100), chance);
Validate.isInCloseInterval(chance, 0, 100);
return nextInt(0, 100) < chance;
}

public static boolean percentageChance(int chance, Random random) {
return percentageChance(random.nextInt(0, 100), chance);
Validate.isInCloseInterval(chance, 0, 100);
return random.nextInt(100) < chance;
}

public static boolean fractionChance(int denominator) {
Validate.isPositive(denominator);
return nextInt(0, denominator) == 0;
}

public static boolean fractionChance(int denominator, Random random) {
Validate.isPositive(denominator);
return random.nextInt(denominator) == 0;
}

public static boolean fractionChance(int numerator, int denominator) {
Validate.isFractionChance(numerator, denominator);
return nextInt(0, denominator) < numerator;
}

public static boolean fractionChance(int numerator, int denominator, Random random) {
Validate.isFractionChance(numerator, denominator);
return random.nextInt(denominator) < numerator;
}

Expand Down

0 comments on commit f5c4d18

Please sign in to comment.