Skip to content

Commit e6421fd

Browse files
committed
Merged with upstream?
2 parents f5ee632 + 00d5edb commit e6421fd

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

README.md

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,80 @@
11
# Java-Probability-Collection
2-
Generic and Highly Optimised Java Data-Structure for Retrieving Random Elements With Probability
2+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/lewysDavies/Java-Probability-Collection/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/lewysDavies/Java-Probability-Collection/?branch=master) [![Build Status](https://scrutinizer-ci.com/g/lewysDavies/Java-Probability-Collection/badges/build.png?b=master)](https://scrutinizer-ci.com/g/lewysDavies/Java-Probability-Collection/build-status/master) [![](https://jitpack.io/v/lewysDavies/Java-Probability-Collection.svg)](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+
```

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ public void test_probability() {
140140
collection.add("C", 10);
141141

142142
int a = 0, b = 0, c = 0;
143-
int totalGets = 100000;
143+
144+
int totalGets = 100_000;
144145

145146
for(int i = 0; i < totalGets; i++) {
146147
String random = collection.get();

0 commit comments

Comments
 (0)