code: public static int randomSegment(int[] probability) {
          int total = 0;
          for (int i = 0; i < probability.length; i++) {
              total += probability[i];
              probability[i] = total;
          }
          int rand = (int) random(0, total - 1);
          for (int i = 0; i < probability.length; i++) {
              if (rand < probability[i]) {
                  return i;
              }
          }
          return -1;
      }
docs: Returns the index of array that specifies probability.
  
  @param probability The element of array represents the probability.
  @return The index of array.