code: public static int getWeightedIndex (int[] weights, Random r)
      {
          int sum = IntListUtil.sum(weights);
          if (sum < 1) {
              return -1;
          }
          int pick = getInt(sum, r);
          for (int ii = 0, nn = weights.length; ii < nn; ii++) {
              pick -= weights[ii];
              if (pick < 0) {
                  return ii;
              }
          }
          log.warning("getWeightedIndex failed", new Throwable()); // Impossible!
          return 0;
      }
docs: Pick a random index from the array, weighted by the value of the corresponding array
  element.
  
  @param weights an array of non-negative integers.
  @param r the random number generator to use
  
  @return an index into the array, or -1 if the sum of the weights is less than 1.  For
  example, passing in {1, 0, 3, 4} will return:
  
  <pre>{@code
  0 - 1/8th of the time
  1 - never
  2 - 3/8th of the time
  3 - half of the time
  }</pre>