source: anac2020/ShineAgent/src/main/java/shineagent/RandomCollection.java

Last change on this file was 1, checked in by wouter, 4 years ago

#1910 added anac2020 parties

File size: 757 bytes
Line 
1package shineagent;
2
3import java.util.*;
4
5public class RandomCollection<E> {
6 private final NavigableMap<Double, E> map = new TreeMap<Double, E>();
7 private final Random random;
8 private double total = 0;
9
10 public RandomCollection() {
11 this(new Random());
12 }
13
14 public RandomCollection(Random random) {
15 this.random = random;
16 }
17
18 public RandomCollection<E> add(double weight, E result) {
19 if (weight <= 0) return this;
20 total += weight;
21 map.put(total, result);
22 return this;
23 }
24
25 public E next() {
26 double value = random.nextDouble() * total;
27 return map.higherEntry(value).getValue();
28 }
29
30 public boolean isEmpty()
31 {
32 return (map.size() == 0);
33 }
34}
Note: See TracBrowser for help on using the repository browser.