class MapWeightRandom<K, V extends Number> {
    private TreeMap<Double, K> weightMap = new TreeMap<Double, K>();

    public MapWeightRandom(List<Pair<K, V>> pairlist) {
        for (Pair<K, V> pair : pairlist) {
            double value = pair.getValue().doubleValue();
            if (value <= 0) {
                continue;
            }

            double lastWeight = weightMap.size() == 0 ? 0 : weightMap.lastKey().doubleValue();
            weightMap.put(value + lastWeight, pair.getKey());
        }
    }

    public K random() {<FILL_FUNCTION_BODY>}
}

        if (MapUtils.isEmpty(weightMap)) {
            throw new DiscoveryException("Weight values are all <= 0 or invalid format");
        }

        double randomWeight = weightMap.lastKey() * Math.random();
        SortedMap<Double, K> tailMap = weightMap.tailMap(randomWeight, false);

        return weightMap.get(tailMap.firstKey());
    
<no_super_class>