@@ -0,0 +1,772 @@
+package dev.kkorolyov.pancake.math;%0A%0Aimport java.util.NavigableMap;%0Aimport java.util.Random;%0Aimport java.util.TreeMap;%0A%0A/**%0A * A distribution of randomly-selectable weighted values.%0A */%0Apublic class WeightedDistribution%3CT%3E %7B%0A%09private final NavigableMap%3CInteger, T%3E distribution = new TreeMap%3C%3E();%0A%09private int total;%0A%09private final Random rand = new Random();%0A%0A%09/**%0A%09 * Adds a weighted value to this distribution.%0A%09 * @param weight value weight relative to this retriever's total weight%0A%09 * @param value added value%0A%09 */%0A%09public void add(int weight, T value) %7B%0A%09%09distribution.put(total, value);%0A%09%09total += weight;%0A%09%7D%0A%0A%09/** @return random value from this distribution */%0A%09public T get() %7B%0A%09%09return distribution.floorEntry(rand.nextInt(total))%0A%09%09%09%09%09%09%09%09%09%09%09 .getValue();%0A%09%7D%0A%7D%0A

Add WeightedDistribution