题目:

\(6\) 停止。求在骰子只出现过偶数的条件下扔骰子次数的期望。

题解:

\(\frac{3}{2}\)。

问题的关键在于要意识到,扔出奇数后相当于实验马上失败了,而不是这次扔出的奇数无效,重新扔。

\(6\) 的条件下扔骰子次数的期望。

\(6\) 出现之前都是偶数这个事件,有:

$$\Pr[\mathcal{E}] = \frac{1}{6} \times \left (1 + \frac{1}{3} + \left( \frac{1}{3}\right)^2 + \cdots \right) = \frac{1}{4}.$$

\(6\) 出现的位置,直接代入定义有:

$$\mathbb{E}X[f(X) | \mathcal{E}] = \frac{1}{\Pr[\mathcal{E}]} \int{x \in \mathcal{E}} \Pr[X = x] f(x) \mathrm{d} x = 4 \times \frac{1}{6} \times \sum_{i=1} \left( \frac{1}{3} \right)^{i-1} i = \frac{3}{2}.$$

代码验证:

#include <bits/stdc++.h>

using namespace std;

int random_roll() {
  int n = rand() % 6 + 1;
  // std::cout << n << '\n';
  return n;
}
double game(int cnt = 0) {

  if(cnt == 0) cnt = 1;
  int t = random_roll();
  if(t % 2 == 1) { // 奇
    return 0;
  }
  else if(t == 6) {
    return cnt;
  }
  else {
    return game(++cnt);
  }
}
double expect(int n) {
  double fenzi = 0.0, fenmu = 0.0;
  for(int i = 1; i <= n; i++) {
    double t = game(); // 扔出奇数时为 0
    // std::cout << "t = " << t << '\n';
    if(t == 0) {
      continue;
    }
    else {
      fenzi += t;
      fenmu += 1.0;  // 扔出偶数时加一
    }
  }
 // 假设你不断扔一个等概率的六面骰子,直到扔出6停止。求在骰子只出现过偶数的概率? 1/4 = 0.25
  printf("%.8f\n", fenmu / n);
  return fenzi / fenmu;
}
int main(int argc, char const *argv[]) {
  srand((unsigned int)time(0));
  printf("%.8f\n", expect(10000)); // 3 / 2 = 1.5
  return 0;
}

相关文章:

  • 2021-09-14
  • 2022-12-23
  • 2019-02-22
  • 2021-06-13
  • 2021-11-12
猜你喜欢
  • 2021-08-24
  • 2021-10-03
  • 2022-01-11
  • 2021-07-01
  • 2021-08-28
  • 2021-09-21
  • 2022-12-23
相关资源
相似解决方案