在 Youtube 视频Numberphile Infinity(从 6:09 开始)中描述了一个实验(圣彼得堡悖论),其中游戏具有无限的期望值。
我尝试编写一个 Java 程序来找出长期预期值(平均值)。理论上的期望是无穷大,但我得到的是10左右的一些数字。是因为我的程序错了吗?因为我认为当实验数量足够大时,实验平均值会非常接近数学期望。这是我的程序。
public class Main {
public static final int NUM_EXPERIMENT = 1000000;
public static void main(String[] args) {
int total = 0;
for (int i = 0; i < NUM_EXPERIMENT; i++) {
int counter = 1;
int subtotal = 1;
while ((int) (Math.random() * 2) == 0) {
subtotal *= 2;
counter++;
}
total += subtotal;
}
double expectation = total / (double) NUM_EXPERIMENT;
System.out.println("The expectation of this experiment is " + expectation);
}
}