Botre Posted April 27, 2015 Posted April 27, 2015 (edited) Leeched from: http://stackoverflow.com/a/6409791 import java.util.NavigableMap; import java.util.Random; import java.util.TreeMap; /** * @author Bjorn Krols (Botre) * @version 0.0 * @since April 27, 2015 */ public class RandomCollection<Generic> { private final NavigableMap<Double, Generic> map; private final Random random; private double total = 0; public RandomCollection() { this(new Random()); } public RandomCollection(Random random) { this.random = random; map = new TreeMap<Double, Generic>(); } public void add(double weight, Generic result) { if (weight <= 0) return; total += weight; map.put(total, result); } public Generic get() { double value = random.nextDouble() * total; return map.ceilingEntry(value).getValue(); } } public static void main(String[] args) { RandomCollection<String> rc = new RandomCollection<String>(); rc.add(1, "1"); rc.add(1, "2"); System.out.println(rc.get()); } Edited April 27, 2015 by Botre 1
Eagle Scripts Posted April 27, 2015 Posted April 27, 2015 Leeched from: http://stackoverflow.com/a/6409791 import java.util.NavigableMap; import java.util.Random; import java.util.TreeMap; /** * @author Bjorn Krols (Botre) * @version 0.0 * @since April 27, 2015 */ public class RandomCollection<Generic> { private final NavigableMap<Double, Generic> map; private final Random random; private double total = 0; public RandomCollection() { this(new Random()); } public RandomCollection(Random random) { this.random = random; map = new TreeMap<Double, Generic>(); } public void add(double weight, Generic result) { if (weight <= 0) return; total += weight; map.put(total, result); } public Generic get() { double value = random.nextDouble() * total; return map.ceilingEntry(value).getValue(); } } public static void main(String[] args) { RandomCollection<String> rc = new RandomCollection<String>(); rc.add(1, "1"); rc.add(1, "2"); System.out.println(rc.get()); } Hhm, what exactly does this do.
7331337 Posted April 27, 2015 Posted April 27, 2015 @ragfr00b It's for basically having a meh random don't know why people would use this as it's much more simpler to keep track of em using a random if pseudo every couple of runs.