protected CandidateGenerator getRandomGenerator(BalancerClusterState cluster) {
    // Prefer conditional generators if they have moves to make
    if (balancerConditionals.isConditionalBalancingEnabled()) {
      for (RegionPlanConditional conditional : balancerConditionals.getConditionals()) {
        List<RegionPlanConditionalCandidateGenerator> generators =
          conditional.getCandidateGenerators();
        for (RegionPlanConditionalCandidateGenerator generator : generators) {
          if (generator.getWeight(cluster) > 0) {
            return generator;
          }
        }
      }
    }

    List<Class<? extends CandidateGenerator>> generatorClasses = shuffledGeneratorClasses.get();
    List<Double> partialSums = new ArrayList<>(generatorClasses.size());
    double sum = 0.0;
    for (Class<? extends CandidateGenerator> clazz : generatorClasses) {
      double weight = weightsOfGenerators.getOrDefault(clazz, 0.0);
      sum += weight;
      partialSums.add(sum);
    }

    // If the sum of all weights is zero, fall back to any generator
    if (sum == 0.0) {
      return pickAnyGenerator(generatorClasses);
    }

    double rand = ThreadLocalRandom.current().nextDouble();
    // Normalize partial sums so that the last one should be exactly 1.0
    for (int i = 0; i < partialSums.size(); i++) {
      partialSums.set(i, partialSums.get(i) / sum);
    }

    // Generate a random number and pick the first generator whose partial sum is >= rand
    for (int i = 0; i < partialSums.size(); i++) {
      if (rand <= partialSums.get(i)) {
        return candidateGenerators.get(generatorClasses.get(i));
      }
    }

    // Fallback: if for some reason we didn't return above, return any generator
    return pickAnyGenerator(generatorClasses);
  }
Select the candidate generator to use based on the cost of cost functions. The chance of
selecting a candidate generator is proportional to the share of cost of all cost functions
among all cost functions that benefit from it.
hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/balancer/StochasticLoadBalancer.java