三个门背后选礼物的问题

2023-08-12  本文已影响0人  CodingCode

闲来无事,测试一下门背后选礼物的问题。

假设有三扇,其中一扇门后面有一个礼物,现在让你选择是哪一扇门,然后主持人在剩下的两扇门里打开了一扇空门;现在问题是,当你发现主持人打开的门是空门后,你要不要重新做选择另一扇门。

这是一个概率问题。

  1. 首先从三扇门任何一个,选中的概率是33.33%。
  2. 当主持人帮你打开一扇门之后,概率会不会发生变化?
    当主持人打开空门之后,剩下两扇门,任意选概率都是50%,此时要不要重新选呢?

写了一个程序测试一下:

     1  #include <stdio.h>
     2  #include <stdlib.h>
     3  #include <time.h>
     4
     5  int switchchoice(int choice, int open) {
     6      if        (choice == 1 && open == 2) {
     7           return 3;
     8      } else if (choice == 1 && open == 3) {
     9           return 2;
    10      } else if (choice == 2 && open == 1) {
    11           return 3;
    12      } else if (choice == 2 && open == 3) {
    13           return 1;
    14      } else if (choice == 3 && open == 1) {
    15           return 2;
    16      } else if (choice == 3 && open == 2) {
    17           return 1;
    18      }
    19
    20      printf("ERROR\n");
    21      return -1;
    22  }
    23
    24  int main() {
    25      int total = 10000;
    26      int match = 0;
    27     srand(time(NULL));
    28
    29     for (int i=0; i < total; i++) {
    30        int place  = rand() % 3 + 1;  // put the gift in place
    31        int choice = rand() % 3 + 1; // your choice
    32
    33        int open   = rand() % 3 + 1;  // host to open
    34        while (open == place || open == choice) {
    35           open   = rand() % 3 + 1;
    36        }
    37
    38        choice = switchchoice(choice, open);
    39
    40        if (choice == place) {
    41          match ++;
    42        }
    43     }
    44     printf("%f\n", match * 100.0/total);
    45
    46     return 0;
    47  }

变化发生在line 38行,即是否重新选择。

测试结果表示:

  1. 不重新选,命中概率是33%,合预期。
  2. 重新选,概率会提高到66%左右,很吃惊吧。

所以结论是,要重选。

上一篇 下一篇

猜你喜欢

热点阅读