class RandomWeightedChooser {

    public E choose() {
        double rand = RepeatableRandom.nextDouble() * sumOfWeights;
        double cumulativeSum = 0;
        for (Entry<E, Double> entry : itemsByWeight.entrySet()) {
            cumulativeSum += entry.getValue();
            if (cumulativeSum > rand) { // Not ">=" to avoid choosing zero weight items when rand is 0!
                return entry.getKey();
            }
        }
        throw Failer.fail("Failed to make a random choice between specified elements");
    }

    public  RandomWeightedChooser(ImmutableMap<E, Double> itemsByWeight);

}