一道有争议的几率题

起缘

昨天 @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

相关文章
相关标签/搜索