Created
July 13, 2017 02:12
-
-
Save mindon/938a3d70fa65b8d13f210ae5259c9863 to your computer and use it in GitHub Desktop.
The Monty Hall Problem 蒙提霍尔问题验证
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // The Monty Hall problem | |
| // 蒙提霍尔问题 | |
| var posibilities = [ | |
| ['C', 'A', 'B'], | |
| ['C', 'B', 'A'], | |
| ['B', 'C', 'A'], | |
| ['B', 'A', 'C'], | |
| ['A', 'B', 'C'], | |
| ['A', 'C', 'B'], | |
| ];// Apple, Banana, Car | |
| function lucky(n) { | |
| var c = [0, 0]; // [original-choice, another] | |
| for(var j=0; j<n; j++) { | |
| var picked = Math.floor(Math.random() * 3); // random original-choice 原选择随机 | |
| var i = Math.floor(Math.random() * 6); // random situation 场景随机 | |
| var t = posibilities[i].slice(); | |
| // opened by Monty Hall | |
| var k = (picked+1)%3; | |
| if(t[k] != 'C') { | |
| t.splice(k, 1); | |
| if(k<picked) picked--; | |
| } else { | |
| k = (picked+2)%3; | |
| if(t[k] != 'C') { | |
| t.splice(k, 1); | |
| if(k<picked) picked--; | |
| } | |
| } | |
| // count lucky 幸运计数 | |
| if(t[picked] == 'C') { // original choice is Car 保持原来的选择中奖次数 | |
| c[0]++; | |
| } else if(t[1-picked] == 'C') { // switch to another got Car 改选另外一个中奖次数 | |
| c[1]++; | |
| } | |
| } | |
| return {"original-choice": c[0]/n, "change-to-another": c[1]/n}; | |
| } | |
| // print result 打印结果 | |
| console.log(lucky(10000)); | |
| // 结果示例 {original-choice: 0.3386, change-to-another: 0.6614} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment