Seeing a video about the “three-door problem”, the first impression is that the conclusion of the video is wrong. I wanted to laugh it off, but after reading the comments, I was confused: what is the answer to the three questions?

As a diligent and inquisitive coder, he felt uncomfortable not knowing the final answer, so he researched deeply and found that “the clown was himself”. If you want to challenge yourself, skip the reasoning and conclusion and come up with an answer yourself, then see if it’s right.

## A circle of friends

After spending an hour trying to figure out the three questions, I posted a post like this:

Three doors problem: There are three doors, one of which is behind a car and the other two are goats. After you choose a door, the host opens one of the other two doors with a goat in it. So, will changing the door at this time increase the probability of obtaining the car?

First mistake: intuition, there is a 1/2 chance of either switching or not switching; Almost stopped there and concluded that it was all a lie.

Second mistake: enumerate, (choose 1, go to 2, change), (choose 2, go to 1, change), (choose 3, go to 1, do not change), (choose 3, go to 2, do not change), it seems that the probability is still 1/2. But I made the mistake of not introducing the preferred probability, which is that instead of counting the last two cases as a quarter, you can only count them as a sixth.

Third introduction probability: 1/3 (choose 1, go 2, change), 1/3 (choose 2, go 1, change), 1/6 (1/3 * 1/2) (choose 3, go 1, do not change), 1/6 (1/3 * 1/2) (choose 3, go 2, do not change), the last two terms combined only 1/3 probability.

So, the answer to the three questions is: choose to change. The probability goes from 1/3 to 2/3;

Through this question, I think: sometimes, persistence may be wrong, may be subjective judgment, the environment may have changed; But sometimes to insist, to insist on the answer of doubt, constantly looking for the answer.

If the underlying logic is: stick to the dynamic view of the problem. That is, men should be shaven with each other for three days.

After sending this circle of friends, I felt it was necessary to realize this problem through the program, and at the same time WROTE an article to share it, so I had this article.

If the above analysis did not understand, it does not matter, the following code analysis practice.

# Three problems

The three questions are from the American television game show Let’s Make a Deal, named after the show’s host Monty Hall.

Problem scenario:

Contestants see three closed doors, one of which has a car behind it. The door with the car behind it wins the car. The other two doors each have a goat hidden behind them. When the contestants chose a door but did not open it, the host opened one of the two remaining doors, revealing one of the goats. The host then asks the contestants if they want to switch to another door that is still closed. The question is: would switching to a different door increase a contestant’s chances of winning the car?

It is said that 90% of them chose not to switch. What are your options?

## Probability analysis

First look at the picture below, there are three doors: car, goat 1 and Goat 2:

The probability of players choosing three gates is 1/3, and specific assumptions are made as follows:

- If the contestant chooses Goat 1, then the host can only hit Goat 2, because the door with a car cannot be opened. The probability of this happening is: 1/3 (contestant’s chance to choose Goat 1) * 1 (host’s choice is certain) = 1/3; If you change at this point, you win the car;
- If the contestant chooses Goat 2, then the host can only fight goat 1 because the door with a car cannot be opened. The probability of this happening is: 1/3 (contestant’s chance to choose Goat 2) * 1 (host’s choice is certain) = 1/3; If you change at this point, you win the car;
- Assuming the contestant chooses the car, the host has two options to open: Goat 1 and Goat 2. Probability of host choosing Goat 1:1/3 (contestant choosing car) * 1/2 (host choosing one) = 1/6; Probability of host choosing Goat 2:1/3 (contestant choosing car) * 1/2 (host choosing one) = 1/6; So, when the contestant chooses the door of the car, the probability of that happening is: 1/3 * 1/2 + 1/3 * 1/2 = 1/3. If you don’t change at this point, you win the car;

Obviously, with a 1/3 chance of each of these things happening, you’re twice as likely to win the car if you switch than if you don’t. In other words, the probability of winning the car becomes 2/3.

## demo

Do the theoretical analysis above, write a code below, to verify:

`Public class ThreeDoors {/** * private static final Random Random = new Random(); Private static int SUCCESS_COUNT = 0; Private static final int PLAY_TIMES = 100000; private static final int PLAY_TIMES = 100000; Public static void main(String[] args) {public static void main(String[] args) { i < PLAY_TIMES; i++) { playGame(); } // Calculate the probability of selecting "change" BigDecimal yield = new BigDecimal(SUCCESS_COUNT). Divide (new BigDecimal(PLAY_TIMES), 4, RoundingMode.HALF_UP) .multiply(new BigDecimal(100)); System.out.println(" execute "+ PLAY_TIMES +", "yield + "%"); } public static void playGame() {public static void playGame() {public static void playGame(); True: Yes Boolean pickedDoor; // Whether the last remaining door is a car, true: yes Boolean leftDoor; Switch (pickDoor(3)) {case 1: door1 = true; break; case 2: door2 = true; break; case 3: door3 = true; break; Default: system.out.println (" abnormal value "); break; Int playerPickedDoor = pickDoor(3); If (playerPickedDoor == door1) {// pickedDoor = door1; // pickedDoor = door1; If (door2) {leftDoor = door2; } else if (door3) {leftDoor = door3; } else {if (pickDoor(2) == 1) {leftDoor = door2; } else { leftDoor = door3; }} else if (playerPickedDoor == door2) {// pickedDoor = door2; If (door1) {leftDoor = door1; } else if (door3) {// If door3 has a car, then only remove door 1 leftDoor = door3; } else {if (pickDoor(2) == 1) {leftDoor = door1; } else { leftDoor = door3; }}} else {// pickedDoor = door3; If (door1) {leftDoor = door1; } else if (door2) {// If door2 has a car, then only remove door 1 leftDoor = door2; } else {if (pickDoor(2) == 1) {leftDoor = door1; } else { leftDoor = door2; PickedDoor = leftDoor; pickedDoor = leftDoor; If (pickedDoor) {SUCCESS_COUNT++; Public static int pickDoor(int bound) {return random.nextint (bound) + 1; }}Copy the code`

The above implementation method, for the time being, does not consider algorithm optimization, just simple case judgment processing.

The above implementation is divided into the following steps:

- Step 1: Randomly select a door and put it into the car. Random Random number is used here. If the car is behind the corresponding door, the corresponding value is set to true.
- Step 2: The contestant chooses a door, and the algorithm still adopts Random number.
- Step 3: On the premise that contestants choose a door, the host removes a door without a car. Instead of dealing with the removed door, the value of the door that remains after removal is recorded. If neither door has a car, choose one of the two at random.
- Step 4: the contestant chooses to switch, i.e. the door the contestant chooses becomes the remaining door.
- Step 5: open the door, verify, if successful record once;
- Step 6: After 10W times, calculate the percentage;

The final log is as follows:

`Perform 100000 experiments, select [swap] probability: 66.7500%Copy the code`

Several times, it will be found that almost all of them are between 66% and 67%, indicating that choosing [change] can indeed double the probability of success.

## summary

Finally, review the whole process: I came across a video about “three-door question”, first made intuitive judgment (wrong), sniffed at others’ conclusion, and then found some objections. So I started looking for evidence and finally got the right answer.

As is said in the circle of friends: sometimes, persistence may be wrong, either because of subjective judgment, or because the environment has changed; But sometimes to insist, to insist on the answer of doubt, to the answer of the pursuit.

This should also be the underlying logic of what we do. We should not judge only by feelings, but more by facts. Programmers, in particular, can also use programs to solve similar problems.

At the same time, don’t you find it interesting to use programs to solve some problems in your life?

About the blogger: Author of the technology book SpringBoot Inside Technology, loves to delve into technology and writes technical articles.

Public account: “program new vision”, the blogger’s public account, welcome to follow ~

Technical exchange: Please contact the weibo user at Zhuan2quan