大数定律

举例

2)使用程序模拟投掷过程

``````public class Application {
public static void main(String[] args) {
int count = 100 * 10000;
int oneCount = 0;
int twoCount = 0;
int threeCount = 0;
int fourCount = 0;
int fiveCount = 0;
int sixCount = 0;
for (int i = 1; i <= count; i++) {
int ran = (int) (Math.random() * 6) + 1;
switch (ran) {
case 1:
oneCount = oneCount + 1;
break;
case 2:
twoCount = twoCount + 1;
break;
case 3:
threeCount = threeCount + 1;
break;
case 4:
fourCount = fourCount + 1;
break;
case 5:
fiveCount = fiveCount + 1;
break;
case 6:
sixCount = sixCount + 1;
break;
}
if (i == 100 || i == 10000 || i == 1000000) {
System.out.println("第" + i + "次统计：");
System.out.println("1\t2\t3\t4\t5\t6");
System.out.print(oneCount + "\t");
System.out.print(twoCount + "\t");
System.out.print(threeCount + "\t");
System.out.print(fourCount + "\t");
System.out.print(fiveCount + "\t");
System.out.println(sixCount);
System.out.print(new BigDecimal(oneCount).divide(new BigDecimal(i)) + "\t");
System.out.print(new BigDecimal(twoCount).divide(new BigDecimal(i)) + "\t");
System.out.print(new BigDecimal(threeCount).divide(new BigDecimal(i)) + "\t");
System.out.print(new BigDecimal(fourCount).divide(new BigDecimal(i)) + "\t");
System.out.print(new BigDecimal(fiveCount).divide(new BigDecimal(i)) + "\t");
System.out.println(new BigDecimal(sixCount).divide(new BigDecimal(i)));
}
}
}
}
``````