Skip to content

Commit f5ee632

Browse files
committed
Fixed probability calculation to be more accurate. Deviation now only 1%. This was only in the test. Not the actual lib. Added App.java for an example executable program to view probabilities nicely.
1 parent 50c9d8c commit f5ee632

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/test/java/com/lewdev/probabilitylib/ProbabilityCollectionTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void test_remove_duplicates() {
127127
assertTrue(collection.getTotalProbability() == 0);
128128
}
129129

130-
@RepeatedTest(3000)
130+
@RepeatedTest(1_000_000)
131131
public void test_probability() {
132132
ProbabilityCollection<String> collection = new ProbabilityCollection<>();
133133

@@ -140,7 +140,7 @@ public void test_probability() {
140140
collection.add("C", 10);
141141

142142
int a = 0, b = 0, c = 0;
143-
int totalGets = 10_0000;
143+
int totalGets = 100000;
144144

145145
for(int i = 0; i < totalGets; i++) {
146146
String random = collection.get();
@@ -150,15 +150,15 @@ public void test_probability() {
150150
else if(random.equals("C")) c++;
151151
}
152152

153-
double aProb = (double) collection.getTotalProbability() / 50.0;
154-
double bProb = (double) collection.getTotalProbability() / 25.0;
155-
double cProb = (double) collection.getTotalProbability() / 10.0;
153+
double aProb = 50.0 / (double) collection.getTotalProbability() * 100;
154+
double bProb = 25.0 / (double) collection.getTotalProbability() * 100;
155+
double cProb = 10.0 / (double) collection.getTotalProbability() * 100;
156156

157-
double aResult = (double) totalGets / a;
158-
double bResult = (double) totalGets / b;
159-
double cResult = (double) totalGets / c;
157+
double aResult = a / (double) totalGets * 100;
158+
double bResult = b / (double) totalGets * 100;
159+
double cResult = c / (double) totalGets * 100;
160160

161-
double acceptableDeviation = 0.5; //5 %
161+
double acceptableDeviation = 1;
162162

163163
assertTrue(Math.abs(aProb - aResult) <= acceptableDeviation);
164164
assertTrue(Math.abs(bProb - bResult) <= acceptableDeviation);

0 commit comments

Comments
 (0)