Introduction:

Spring Festival is approaching, send a question about red envelopes. I saw this interesting question on a platform before:

Suppose there are three red packets, but only one of them has money in it. You can choose any one of them. After you choose, I will take out an empty red envelope in the remaining two red envelopes, at this time, there will be two red envelopes left, a rich, a no money. Now give you a chance, will you change your choice? Or will you stick with your choice?

What would you have done? Before reading the discussion below, you may want to think about whether you would change your choice or stick to your choice. Or you think it doesn’t matter, there’s no point in changing it.

Let’s do it visually

When I put forward such interesting questions to people around me, most of the answers I get are that it doesn’t matter whether I change the red envelope or not. The reason is that after removing one red envelope without money, there are still two left, so the probability of choosing either one is 50%. So what does it matter whether I change the red envelope or not?

That seems reasonable, but it’s not. When you chose the red envelope in the first time, your probability was 1/3, so why did I delete the red envelope with no money in the remaining two? In fact, you did nothing, so the probability should still be the same as when you chose the red envelope in the first place.

To take a very extreme example: suppose I have 10,000 red packets and you choose one of them, the probability of winning is 1 in 10,000, which is obviously not likely to be selected. Then I remove 9998 empty red envelopes in the remaining 9999, will you change them? If you still think you have 2 red packets left and it’s 50%, that’s obviously wrong, because you know from the beginning that you’re not likely to win, how can all of a sudden you choose 50%?

So the answer should be: if I had the chance to change, I would choose to change!

Here’s a diagram:

Based on the above analysis, if the choice is changed, then the probability of winning becomes 2/3.

Let’s do it mathematically

Maybe the above expression is not clear enough, or others think it is not convincing enough, so let’s analyze it from the point of view of mathematics.

If I choose the first red envelope each time, I can make a table of the probability of winning:

When I don’t make a replacement:

Location of red envelopes Whether or not the winning
1 is
2 no
3 no

Two no’s and one yes, so 1/3 chance of winning

When I choose to change

Location of red envelopes Whether or not the winning
1 no
2 is
3 is

The odds of winning are 2/3

So maybe the answer is already there. Obviously, if I choose to change the red envelope, the probability of winning is higher, which is 2/3

Analyze it from a practical point of view

The probability is not 100% according to the probability that we calculated the number of times, but it must be close to the theoretical number. Practice is the only standard to test truth, we can test the above theory through time, so that the most convincing.

In order to achieve the ideal practice environment, a lot of experiments must be done, not to mention a waste of time, and subjective factors can interfere. As a programmer, I’m going to let the computer do this, and all we have to do is code!

Design a program

Our goal is to do practice on the problem, so we can start designing the program:

  • Defines an array of packages

  • One of the values in the array will be true and the other two will be false.

  • Randomly select a red envelope: use a random function to generate a selected subscript selectIndex

  • Remove an empty red envelope from the two remaining red envelopes that were not selected: Remove an element from the package whose subscript is not equal to selectIndex and whose value is false

  • Swap/do not swap selectIndexes separately

  • Output the winning results, input a large number of test cases, output the winning probability.

Write a program

According to the above ideas, we can write the following program:

const swap = function (selectIndex) { return selectIndex === 0 ? 1: } const handle = function (isSwap) {const packages = [] const moneyIndex = math.ceil (math.random () * For (let index = 0; let index = 0; let index = 0; index < 3 ; Index++) {teses.push (moneyIndex === index) // true } let selectIndex = math.ceil (math.random () * 3) -1 Const result = packages[selectIndex] for (let index = 0; index < 3; index++) { if (index ! SelectIndex &&packages [index] === false) { Splice (index, 1) selectIndex = index < selectIndex? selectIndex - 1 : selectIndex isSwap ? selectIndex = swap(selectIndex) : Break}} return packages[selectIndex] const res = [] const count = 10000 // Number of tests for (let index = 0 ; index < count; Index++) {res.push(handle(false)) -true swap -false Keep original selection} const trueList = res.filter(item => {return item === true // Fetch all winning test cases}) ${truelist.length}.tofixed (4) * 100}% ') ${(truelist.length/count).tofixed (4) * 100}% ')Copy the code

Since the program is not difficult, I will only provide comments without explanation. We had 10,000 tests, so let’s run it.

It can be found that the probability is generally around 33% when there is no exchange, and 66% after the exchange. Practice and theory are very close.

So do you think you should change your original choice now?