一道有爭議的機率題

起緣

昨天 @vicyang 在閃存發了一條閃,原文以下:html

【爭議的機率題】有三張彩票 只有一張中獎 你買走一張 而後老闆當場開了一張 沒中 給你個機會:你能夠用剩下的一張換你手裏的 換不換? bbs.bathome.net... (我已經在羣裏嚼的很熟了,發過來給各位看看)markdown

另外在百度貼吧中有不少爭論,點此處查看。當時我憑直覺認定交換和不交換中獎機率是同樣的,因而回復了閃存以下:dom

都學成書呆子了。 6-3 22:37post

隨後 @vicyang 帖了驗證代碼上來,ui

兩種狀況的代碼,由於說1/3的那羣人並不能很好的指出咱們哪裏錯了,雙方都用代碼模擬過程才能找出區別所在。spa

當時就感受多是本身錯了,趕忙打開Visual Sudio敲入了代碼....net

模擬

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace IsThisRight
{
    class Program
    {
        static void Main(string[] args) {
            int luckCountA = 0;
            int luckCountB = 0;

            int count = 100000;

            Console.WriteLine("模擬開始");

            for (int i = 0; i <= count; i++)
            {
                int[] lotteryArray = CreateLottery();//隨機產生3張彩票,其中1張有獎
                int selectedIndexOfCustomer = GetRodamNum();//隨機選一張彩票
                GetOneNotLuckLottery(lotteryArray, selectedIndexOfCustomer);//店主拿走一張不中獎的彩票,這句話其實寫不寫都同樣了,下面這個else說明了爲何選擇交換機率變成了2/3

                if (lotteryArray[selectedIndexOfCustomer] == 1)//不交換而中獎
                {
                    luckCountA++;
                }
                else //交換而中獎(由於如今只有2張彩票,其中一張確定有獎,若是不交換未中獎,那麼交換必中獎)
                {
                    luckCountB++;
                }
            }

            Console.WriteLine("");
            Console.WriteLine(String.Format("若是不交換的話,{2}次裏中獎{0}次,中獎率約爲{1}%",luckCountA,luckCountA * 100 / count, count));
            Console.WriteLine(String.Format("若是交換的話,{2}次裏中獎{0}次,中獎率約爲{1}%",luckCountB,luckCountB * 100 / count, count));

            Console.ReadLine();
        }

        private static int GetOneNotLuckLottery(int[] lotteryArray, int selectedIndexOfCustomer) {
            while (true)
            {
                int index = GetRodamNum();
                if (index == selectedIndexOfCustomer)
                {
                    continue;
                }
                if (lotteryArray[index] == 1)
                {
                    continue;
                }
                return index;
            }
        }

        //隨機產生3張彩票。1:中獎;0:未中
        private static int[] CreateLottery()
        {
            int[] array = { 0, 0, 0 };
            int luckIndex = GetRodamNum();
            array[luckIndex] = 1;
            return array;
        }

        private static int GetRodamNum() {
            return new Random(Guid.NewGuid().GetHashCode()).Next(3);
        }
    }
}

模擬結果以下:
此處輸入圖片的描述code

淵源

立刻在閃存裏認錯後又在網上搜索了一下,原來這個問題叫作「三門問題」,起源於一個電視遊戲節目:orm

參賽者會看見三扇關閉了的門,其中一扇的後面有一輛汽車,選中後面有車的那扇門就能夠贏得該汽車,而另外兩扇門後面則各藏有一隻山羊。當參賽者選定了一扇門,但未去開啓它的時候,節目主持人開啓剩下兩扇門的其中一扇,露出其中一隻山羊。主持人其後會問參賽者要不要換另外一扇仍然關上的門。問題是:換另外一扇門會否增長參賽者贏得汽車的機會率嗎?htm

這裏是果殼網上的描述:換仍是不換?爭議從未中止過的三門問題~

結論

不是三門問題的結論(固然,這個問題也有結論了),而是本博文的結論:

直覺有風險,裝66需謹慎

 

 

參考:http://www.cnblogs.com/zzy0471/p/4550719.html

相關文章
相關標籤/搜索