[关闭]
@zzy0471 2015-06-03T17:00:41.000000Z 字数 1911 阅读 3136

一道有争议的概率题

算法 概率


起缘

一切都因昨天在闪存装66而起。昨天 @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 //交换而中奖
                {
                    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需谨慎

福利

好像现在写博文流行送福利,咱也不能免俗,现奉上来自汽车之家的一个帖子,当然,汽车不是重点。

提车半年,附女友图

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注