起緣
昨天 @vicyang 在閃存發了一條閃,原文如下:
【爭議的概率題】有三張彩票 只有一張中獎 你買走一張 然后老板當場開了一張 沒中 給你個機會:你可以用剩下的一張換你手里的 換不換? bbs.bathome.net... (我已經在群里嚼的很熟了,發過來給各位看看)
另外在百度貼吧中有很多爭論,點此處查看。當時我憑直覺認定交換和不交換中獎概率是一樣的,於是回復了閃存如下:
都學成書呆子了。 6-3 22:37
隨后 @vicyang 帖了驗證代碼上來,當時就感覺可能是自己錯了,趕緊打開Visual Sudio敲入了代碼...
模擬
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);
}
}
}
模擬結果如下:
淵源
馬上在閃存里認錯后又在網上搜索了一下,原來這個問題叫做“三門問題”,起源於一個電視游戲節目:
參賽者會看見三扇關閉了的門,其中一扇的后面有一輛汽車,選中后面有車的那扇門就可以贏得該汽車,而另外兩扇門后面則各藏有一只山羊。當參賽者選定了一扇門,但未去開啟它的時候,節目主持人開啟剩下兩扇門的其中一扇,露出其中一只山羊。主持人其后會問參賽者要不要換另一扇仍然關上的門。問題是:換另一扇門會否增加參賽者贏得汽車的機會率嗎?
這里是果殼網上的描述:換還是不換?爭議從未停止過的三門問題~
結論
不是三門問題的結論(當然,這個問題也有結論了),而是本博文的結論:
直覺有風險,裝66需謹慎。
福利
福利已刪除。