public Integer evaluate(Double... vals) {
    double sum = 0.0;
    for (int ii = 0; ii < vals.length; ++ii) {
      Double v = vals[ii];
      if (v == null || v < 0 || v.isNaN() || v.isInfinite()) {
        return null;
      }
      sum += vals[ii];
    }

    double r = Math.random() * sum ;
    for (int ii = 0; ii < vals.length; ++ii) {
      if (r < vals[ii]) {
        return Integer.valueOf(ii);
      }
      r -= vals[ii] ;
    }

    // In case of floating point precision issues, return index.
    return Integer.valueOf(vals.length - 1);
  }