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