/** * Samples a random index according to the given proportions. Note that * calling this requires calculating the sum of the proportions first, so * if it is known in advance, it is more efficient to call the version of * this method that takes the sum of the proportions as a parameter. * * @param random * The random number generator to use. * @param proportions * The array of proportions. All entries must be greater than or * equal to zero. * @return * A random index sampled according to the given proportions. */ public static int sampleIndexFromProportions( final Random random, final double[] proportions) { // To sample from the proportions we will need the sum. double proportionSum = 0.0; for (double value : proportions) { proportionSum += value; } return sampleIndexFromProportions(random, proportions, proportionSum); }
/** * Samples a random index according to the given proportions. Note that * calling this requires calculating the sum of the proportions first, so * if it is known in advance, it is more efficient to call the version of * this method that takes the sum of the proportions as a parameter. * * @param random * The random number generator to use. * @param proportions * The array of proportions. All entries must be greater than or * equal to zero. * @return * A random index sampled according to the given proportions. */ public static int sampleIndexFromProportions( final Random random, final double[] proportions) { // To sample from the proportions we will need the sum. double proportionSum = 0.0; for (double value : proportions) { proportionSum += value; } return sampleIndexFromProportions(random, proportions, proportionSum); }
/** * Samples a random index according to the given proportions. Note that * calling this requires calculating the sum of the proportions first, so * if it is known in advance, it is more efficient to call the version of * this method that takes the sum of the proportions as a parameter. * * @param random * The random number generator to use. * @param proportions * The array of proportions. All entries must be greater than or * equal to zero. * @return * A random index sampled according to the given proportions. */ public static int sampleIndexFromProportions( final Random random, final double[] proportions) { // To sample from the proportions we will need the sum. double proportionSum = 0.0; for (double value : proportions) { proportionSum += value; } return sampleIndexFromProportions(random, proportions, proportionSum); }
final int randomIndex = sampleIndexFromProportions( random, proportions, proportionSum); return new int[] { randomIndex };
final int randomIndex = sampleIndexFromProportions( random, proportions, proportionSum); return new int[] { randomIndex };
final int randomIndex = sampleIndexFromProportions( random, proportions, proportionSum); return new int[] { randomIndex };