sclass RandomizerFromHistogram implements IF0 {
new LPair list;
transient Random randomizer = defaultRandomizer();
*(MultiSet ms) {
long sum = 0;
for (A a : keys(ms)) {
int val = ms.get(a);
if (val == 0) continue;
sum += val;
list.add(pair(a, sum));
}
}
public A get() {
if (empty(list)) null;
long val = randomLong(randomizer, last(list).b)+1;
int idx = generalizedBinarySearch2(list, p -> cmp((long) p.b, val));
if (idx < 0) {
print("idx1: " + idx);
idx = -idx-1;
}
//print("idx: " + idx);
ret assertNotNull(pairA(_get(list, idx)));
}
}