diff --git a/src/main/java/com/gmail/nossr50/util/random/Probability.java b/src/main/java/com/gmail/nossr50/util/random/Probability.java index a4d8c848c..52131dac3 100644 --- a/src/main/java/com/gmail/nossr50/util/random/Probability.java +++ b/src/main/java/com/gmail/nossr50/util/random/Probability.java @@ -9,16 +9,28 @@ public interface Probability { /** * The value of this Probability * Should return a result between 0 and 1 (inclusive) - * 1 should represent something that will always succeed - * 0.5 should represent something that succeeds around half the time - * etc + * A value of 1 or greater represents something that will always succeed + * A value of around 0.5 represents something that succeeds around half the time + * A value of 0 represents something that will always fail * * @return the value of probability */ double getValue(); - static @NotNull Probability ofPercent(double percentageValue) { - return new ProbabilityImpl(percentageValue); + /** + * Create a new Probability with the given value + * A value of 100 would represent 100% chance of success + * A value of 50 would represent 50% chance of success + * A value of 0 would represent 0% chance of success + * A value of 1 would represent 1% chance of success + * A value of 0.5 would represent 0.5% chance of success + * A value of 0.01 would represent 0.01% chance of success + * + * @param percentage the value of the probability + * @return a new Probability with the given value + */ + static @NotNull Probability ofPercent(double percentage) { + return new ProbabilityImpl(percentage); } /** @@ -29,7 +41,7 @@ public interface Probability { * @return true for succeeding, false for failing */ static private boolean isSuccessfulRoll(double probabilityValue) { - return (probabilityValue * 100) >= ThreadLocalRandom.current().nextDouble(100D); + return (probabilityValue) >= ThreadLocalRandom.current().nextDouble(1D); } /** diff --git a/src/main/java/com/gmail/nossr50/util/random/ProbabilityImpl.java b/src/main/java/com/gmail/nossr50/util/random/ProbabilityImpl.java index 489805807..e240f2f07 100644 --- a/src/main/java/com/gmail/nossr50/util/random/ProbabilityImpl.java +++ b/src/main/java/com/gmail/nossr50/util/random/ProbabilityImpl.java @@ -10,14 +10,15 @@ public class ProbabilityImpl implements Probability { /** * Create a probability with a static value * - * @param staticProbability the value to assign to this probability + * @param percentage the percentage value of the probability */ - ProbabilityImpl(double staticProbability) throws ValueOutOfBoundsException { - if (staticProbability < 0) { + ProbabilityImpl(double percentage) throws ValueOutOfBoundsException { + if (percentage < 0) { throw new ValueOutOfBoundsException("Value should never be negative for Probability! This suggests a coding mistake, contact the devs!"); } - probabilityValue = staticProbability; + // Convert to a 0-1 floating point representation + probabilityValue = percentage / 100.0D; } ProbabilityImpl(double xPos, double xCeiling, double probabilityCeiling) throws ValueOutOfBoundsException { diff --git a/src/test/java/com/gmail/nossr50/util/random/ProbabilityTest.java b/src/test/java/com/gmail/nossr50/util/random/ProbabilityTest.java index d40182734..a2b6cc416 100644 --- a/src/test/java/com/gmail/nossr50/util/random/ProbabilityTest.java +++ b/src/test/java/com/gmail/nossr50/util/random/ProbabilityTest.java @@ -78,7 +78,7 @@ class ProbabilityTest { @ParameterizedTest @MethodSource("provideProbabilitiesForWithinExpectations") - void testOddsExpectationsImplConstructor(Probability probability, double expectedWinPercent) { + void testOddsExpectationsConstructor(Probability probability, double expectedWinPercent) { assertExpectations(probability, expectedWinPercent); }