code: private static int chooseWithProbability(double[] distances,
                                               double sum,
                                               double probability) {
          for (int j = 0; j < distances.length; ++j) {
              double probOfDistance = Math.pow(distances[j], 2) / sum;
              probability -= probOfDistance;
              if (probability <= EPSILON) {
                  return j;
              }
          }
          return distances.length-1;
      }
docs: Returns a data point index i with probability
  p(distances[i]^2/sum)