项目作者: lewysDavies

项目描述 :
Generic and Highly Optimised Java Data-Structure for Retrieving Random Elements With Probability
高级语言: Java
项目地址: git://github.com/lewysDavies/Java-Probability-Collection.git
创建时间: 2020-07-07T13:47:51Z
项目社区:https://github.com/lewysDavies/Java-Probability-Collection

开源协议:MIT License

下载


Java-Probability-Collection

Scrutinizer Code Quality Build Status

Generic and Highly Optimised Java Data-Structure for Retrieving Random Elements with Probability

Usage

  1. ProbabilityCollection<String> collection = new ProbabilityCollection<>();
  2. collection.add("A", 50); // 50 / 85 (total probability) = 0.588 * 100 = 58.8% Chance
  3. collection.add("B", 25); // 25 / 85 (total probability) = 0.294 * 100 = 29.4% Chance
  4. collection.add("C", 10); // 10 / 85 (total probability) = 0.117 * 100 = 11.7% Chance
  5. String random = collection.get();

Proven Probability

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 1% deviation from the expected probability.

A real world example is provided in ExampleApp.java (within the test folder), Typical Output with 100,000 gets::

  1. Prob | Actual
  2. -----------------------
  3. A: 58.824% | 58.975%
  4. B: 29.412% | 29.256%
  5. C: 11.765% | 11.769%

Performance

Get performance has been significantly improved in comparison to my previous map implementation. This has been achieved with custom compared TreeSets.

  1. Benchmark Mode Cnt Score Error Units
  2. BenchmarkProbability.collectionAddSingle avgt 5 501.688 ± 33.925 ns/op
  3. BenchmarkProbability.collectionGet avgt 5 69.373 ± 2.198 ns/op
  4. BenchmarkProbability.mapAddSingle avgt 5 25809.712 ± 984.980 ns/op
  5. BenchmarkProbability.mapGet avgt 5 902.414 ± 22.388 ns/op

Installation

Super Simple: Copy ProbabilityCollection.java into your project


or for the fancy users, you could use Maven:

Repository:

  1. <repository>
  2. <id>jitpack.io</id>
  3. <url>https://jitpack.io</url>
  4. </repository>

Dependency:

  1. <dependency>
  2. <groupId>com.github.lewysDavies</groupId>
  3. <artifactId>Java-Probability-Collection</artifactId>
  4. <version>v0.8</version>
  5. </dependency>

Maven Shade This Dependency:

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-shade-plugin</artifactId>
  4. <version>3.1.1</version>
  5. <executions>
  6. <execution>
  7. <configuration>
  8. <relocations>
  9. <relocation>
  10. <!-- Avoid Name Conflics -->
  11. <pattern>com.lewdev.probabilitylib</pattern>
  12. <shadedPattern>***<!--YOUR.PACKAGE.HERE-->***.probabilitylib</shadedPattern>
  13. </relocation>
  14. </relocations>
  15. </configuration>
  16. <phase>package</phase>
  17. <goals>
  18. <goal>shade</goal>
  19. </goals>
  20. </execution>
  21. </executions>
  22. </plugin>