code: private static int selectWithProb(double probs[]) {
  	double cutoff = Math.random();
  	double curProbSum = 0.0;
  
  	for (int i = 0; i < probs.length; i++) {
              curProbSum += probs[i];
              // NOTE: the assert is slightly larger than 1 to allow for marginal
              // accumulation due to double round-off errors
              assert curProbSum <= 1.0001
                  : "sum of probabilities > 1: " + curProbSum;
              if (curProbSum >= cutoff)
                  return i;
          }
          throw new IllegalStateException("Probablilities do not sum to 1");
      }
docs: Given an array of probability values (which sum to 1) samples an index in
  {@code prob} according to its probability.