20
Jul/081
Jul/081
羊与车问题计算机模拟
先推荐一下Mathematica,这个东西擅长数学运算,速度快结果精确,在许多情况下可以取代程序设计语言,比如这次我要解决的问题--羊与车问题
题目描述(来自Matrix67的博客)
在一个游戏中有三个门,只有一个门后面有车,另外两个门后面是羊.你想要车,但你不知道哪一个门后面有车.主持人让你随便选了一个门.比如说,你选择了1号门.但你还不知道你是否选到了车.然后主持人打开了另一扇门,比如3号.你清楚地看到3号门后面是一只羊.现在主持人给你一个改变主意的机会.请问你是否会换选成2号门?
嗯,概率问题永远是计算机的强项...借此贴抛砖引玉,希望大家提供更多Mathematica的入门案例
(* Simulation for Problem Monty Hall Dilemma *)
simTimes = 1000000;
got = 0;
For [ i = 0, i < simTimes, i++,
(* Door No. of Car *)
car = RandomInteger[{1, 3}];
(* Player's Choice *)
choice = RandomInteger[{1, 3}];
(* If they're the same
it means that if player changed his or her mind
he or she will lose the game *)
If[car != choice, got++];
]
N[got/simTimes, 5]
Comments (1)
Trackbacks (0) ( subscribe to comments on this post )
Leave a comment
No trackbacks yet.
9:22 am on July 21st, 2008
基本瞎蒙