随机数
证明,等概率随机
@Test
public void equalProbabilityRandom() {
int testTimes = 10000000;
int count = 0;
for (int i = 0; i < testTimes; i++) {
double random = Math.random();
if (random < 0.5) {
count++;
}
}
System.out.println("随机概率为:" + ((double)count / (double) testTimes)); // 0.5000338
}将 [0, x) 出现的概率由 x 变为 x^2
@Test
public void equalProbabilityRandom() {
int testTimes = 10000000;
int count = 0;
for (int i = 0; i < testTimes; i++) {
double random = Math.max(Math.random(), Math.random());
if (random < 0.5) {
count++;
}
}
System.out.println("随机概率为:" + ((double)count / (double) testTimes)); // 0.2501026
}面试题
从15随机到17随机
从01不等概率随机到01等概率随机
最后更新于