我运行以下程序,典型的控制台输出如下。
加权 0 的
平均百分比为:57.935590153643616 加权 1 的平均百分比为:42.06440984635654
为什么这些打印的平均值不接近 60 和 40?
public static void main(String[] args) {
Random rand = new Random();
int numCycles = 5000;
double[] weightings = {60.0, 40.0};
double[] weightedRandoms = new double[weightings.length];
double[] totPercentagePoints = {0.0, 0.0};
for (int j = 0; j < numCycles; j++) {
for (int k = 0; k < weightings.length; k++) {
weightedRandoms[k] = (rand.nextInt(10) + 1) * weightings[k]; // +1 to the random integer to ensure that the weighting is not multiplied by 0
}
for (int k = 0; k < weightings.length; k++) {
totPercentagePoints[k] += weightedRandoms[k] / DoubleStream.of(weightedRandoms).sum() * 100;
}
}
for (int i = 0; i < weightings.length; i++) {
System.out.println("Mean percentage points for weighting " + i + " is: " + totPercentagePoints[i] / numCycles);
}
}