public <T> T pick (Iterator<? extends T> values, T ifEmpty, Weigher<? super T> weigher)
    {
        T pick = ifEmpty;
        double total = 0.0;
        while (values.hasNext()) {
            T val = values.next();
            double weight = weigher.getWeight(val);
            if (weight > 0.0) {
                total += weight;
                if ((total == weight) || ((_r.nextDouble() * total) < weight)) {
                    pick = val;
                }
            } else if (weight < 0.0) {
                throw new IllegalArgumentException("Weight less than 0: " + val);
            }
            // else: weight == 0.0 is OK
        }
        return pick;
    }
public < T > T pick ( Iterator < ? extends T > values , T ifEmpty , Weigher < ? super T > weigher ) { T pick = ifEmpty ; double total = 0.0 ; while ( values . hasNext ( ) ) { T val = values . next ( ) ; double weight = weigher . getWeight ( val ) ; if ( weight > 0.0 ) { total += weight ; if ( ( total == weight ) || ( ( _r . nextDouble ( ) * total ) < weight ) ) { pick = val ; } } else if ( weight < 0.0 ) { throw new IllegalArgumentException ( "Weight less than 0: " + val ) ; } } return pick ; }
Pick a random element from the specified iterator, or return
<code>ifEmpty</code> if no element has a weight greater than <code>0</code>.

<p><b>Implementation note:</b> a random number is generated for every entry with a
non-zero weight after the first such entry.

@throws NullPointerException if values is null.
@throws IllegalArgumentException if any weight is less than 0.