1 package org.opentrafficsim.core.math;
2
3 import java.util.Collection;
4 import java.util.Iterator;
5 import java.util.Map;
6
7 import nl.tudelft.simulation.jstats.streams.StreamInterface;
8 import nl.tudelft.simulation.language.Throw;
9
10
11
12
13
14
15
16
17
18
19
20
21 public final class Draw
22 {
23
24
25 private Draw()
26 {
27
28 }
29
30
31
32
33
34
35
36
37 public static <E> E drawWeighted(final Map<E, ? extends Double> map, final StreamInterface stream)
38 {
39 Throw.whenNull(map, "Map may not be null.");
40 Throw.whenNull(stream, "Stream may not be null.");
41 Throw.when(map.isEmpty(), IllegalArgumentException.class, "Map may not be empty.");
42 double sumProb = 0.0;
43 for (E e : map.keySet())
44 {
45 double w = map.get(e);
46 Throw.when(w < 0.0, IllegalArgumentException.class, "Probabilities should be at least 0.0.");
47 sumProb += w;
48 }
49 Throw.when(sumProb == 0.0, IllegalArgumentException.class, "Probabilities should not add to 0.0.");
50
51 if (map.size() == 1)
52 {
53 return map.keySet().iterator().next();
54 }
55 double r = stream.nextDouble() * sumProb;
56 sumProb = 0.0;
57 E last = null;
58 for (E e : map.keySet())
59 {
60 double f = map.get(e);
61 if (sumProb <= r && r <= sumProb + f)
62 {
63 return e;
64 }
65 sumProb += f;
66 last = e;
67 }
68 return last;
69 }
70
71
72
73
74
75
76
77
78 public static <E> E draw(final Collection<E> collection, final StreamInterface stream)
79 {
80 Throw.whenNull(collection, "Collection may not be null.");
81 Throw.whenNull(stream, "Stream may not be null.");
82 Throw.when(collection.isEmpty(), IllegalArgumentException.class, "Collection may not be empty.");
83 int n = (int) (collection.size() * stream.nextDouble());
84 Iterator<E> it = collection.iterator();
85 int m = 0;
86 while (m < n)
87 {
88 m++;
89 it.next();
90 }
91 return it.next();
92 }
93
94 }