|
1 | 1 | # Java-Probability-Collection |
2 | | -Generic and Highly Optimised Java Data-Structure for Retrieving Random Elements With Probability |
| 2 | +[](https://scrutinizer-ci.com/g/lewysDavies/Java-Probability-Collection/?branch=master) [](https://scrutinizer-ci.com/g/lewysDavies/Java-Probability-Collection/build-status/master) [](https://jitpack.io/#lewysDavies/Java-Probability-Collection)<br> |
| 3 | +Generic and Highly Optimised Java Data-Structure for Retrieving Random Elements with Probability |
| 4 | + |
| 5 | +# Usage |
| 6 | +``` |
| 7 | +ProbabilityCollection<String> collection = new ProbabilityCollection<>(); |
| 8 | +collection.add("A", 50); // 50 / 85 (total probability) = 0.588 * 100 = 58.8% Chance |
| 9 | +collection.add("B", 25); // 25 / 85 (total probability) = 0.294 * 100 = 29.4% Chance |
| 10 | +collection.add("C", 10); // 10 / 85 (total probability) = 0.117 * 100 = 11.7% Chance |
| 11 | +
|
| 12 | +String random = collection.get(); |
| 13 | +``` |
| 14 | + |
| 15 | +# Proven Probability |
| 16 | +The probability test is run **1,000,000 times**. Each time getting **100,000** random elements and counting the spread. The test would not pass if the spread had over **3.5%** deviation from the expected probability. |
| 17 | + |
| 18 | +Here is a real world example which used 1,000 selects: |
| 19 | +``` |
| 20 | +A's Probability is 0.38% | Was selected 0.39% of the time |
| 21 | +B's Probability is 0.38% | Was selected 0.38% of the time |
| 22 | +C's Probability is 0.19% | Was selected 0.18% of the time |
| 23 | +D's Probability is 0.04% | Was selected 0.04% of the time |
| 24 | +RareAF's Probability is 0.01% | Was selected 0.01% of the time |
| 25 | +``` |
| 26 | + |
| 27 | +# Performance |
| 28 | +Get performance has been significantly improved in comparison to my previous map implementation. This has been achieved with custom compared TreeSets. |
| 29 | +0.314ms to just 0.004. |
| 30 | +``` |
| 31 | +Benchmark Mode Cnt Score Error Units |
| 32 | +new_collectionAddSingle avgt 10 0.002 ± 0.001 s/op |
| 33 | +new_collectionGet avgt 10 0.004 ± 0.001 s/op |
| 34 | +old_mapAddSingle avgt 10 0.001 ± 0.001 s/op |
| 35 | +old_mapGet avgt 10 0.314 ± 0.069 s/op |
| 36 | +``` |
| 37 | + |
| 38 | +# Installation |
| 39 | +**Super Simple: Copy ProbabilityCollection.java into your project**<br><br> |
| 40 | +or for the fancy users, you could use Maven:<br> |
| 41 | +**Repository:** |
| 42 | +``` |
| 43 | +<repository> |
| 44 | + <id>jitpack.io</id> |
| 45 | + <url>https://jitpack.io</url> |
| 46 | +</repository> |
| 47 | +``` |
| 48 | +**Dependency:** |
| 49 | +``` |
| 50 | +<dependency> |
| 51 | + <groupId>com.github.lewysDavies</groupId> |
| 52 | + <artifactId>Java-Probability-Collection</artifactId> |
| 53 | + <version>0.5</version> |
| 54 | +</dependency> |
| 55 | +``` |
| 56 | +**Maven Shade This Dependency:** |
| 57 | +``` |
| 58 | +<plugin> |
| 59 | + <groupId>org.apache.maven.plugins</groupId> |
| 60 | + <artifactId>maven-shade-plugin</artifactId> |
| 61 | + <version>3.1.1</version> |
| 62 | + <executions> |
| 63 | + <execution> |
| 64 | + <configuration> |
| 65 | + <relocations> |
| 66 | + <relocation> |
| 67 | + <!-- Avoid Name Conflics --> |
| 68 | + <pattern>com.lewdev.probabilitylib</pattern> |
| 69 | + <shadedPattern>***<!--YOUR.PACKAGE.HERE-->***.probabilitylib</shadedPattern> |
| 70 | + </relocation> |
| 71 | + </relocations> |
| 72 | + </configuration> |
| 73 | + <phase>package</phase> |
| 74 | + <goals> |
| 75 | + <goal>shade</goal> |
| 76 | + </goals> |
| 77 | + </execution> |
| 78 | + </executions> |
| 79 | +</plugin> |
| 80 | +``` |
0 commit comments