Skip to content

Instantly share code, notes, and snippets.

@vace
Created January 15, 2021 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vace/19164ab187b27945cdf4977f53c2f960 to your computer and use it in GitHub Desktop.
Save vace/19164ab187b27945cdf4977f53c2f960 to your computer and use it in GitHub Desktop.
贝叶斯-蒙提霍尔 问题演示脚本
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<!--author:Vace_Vlm(ocdo@qq.com),create:15 Jan 2021 11:17 AM-->
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="renderer" content="webkit">
<meta http-equiv="Cache-Control" content="no-siteapp" />
<meta name="apple-mobile-web-app-title" content="vace" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
<title>验证演示程序</title>
</head>
<body>
<div id="app">
<p>三个门A B C后面 只有一个门放着奖品。主持人让你选。在你做完选择后 (假设你选A),主持人打开另外两扇门中没有奖品的那一扇门 (假设打开了B),这时候问你 你是坚持选 A还是换成C。</p>
<pre id="output"></pre>
</div>
<script>
console.log = function (...args) {
const inner = args.map(t => `<span>${t}</span>`).join(' ') || ' '
const div = document.createElement('div')
div.innerHTML = inner
output.appendChild(div)
}
</script>
<script src="main.js"></script>
<!-- built files will be auto injected -->
</body>
</html>
// node main.js
console.log('❓无论B是否为空,都不更换A选项')
bayes({ count: 10000, isExchange: false, isKnow: false })
console.log('❓只要B是空的就更换为C选项')
bayes({ count: 10000, isExchange: true, isKnow: false })
console.log('❓主持人知道B或者C的空箱,打开空箱则更换为另外一个')
bayes({ count: 10000, isExchange: true, isKnow: true })
function bayes (config) {
const { count, isExchange, isKnow } = config
const A = 0, B = 1, C = 2
const rewards = [0, 0, 1]
const counts = { max: count, hit: 0, change: 0 }
for (var i = 0; i < count; i++) {
shuffle(rewards)
// 用户选择 A 中的奖品
var checked = rewards[A]
if (isExchange) {
// 主持人明确知道B或者C中的空箱
if (isKnow) {
if (!rewards[B]) {
checked = rewards[C]
counts.change += 1
}
else if (!rewards[C]) {
checked = rewards[B]
counts.change += 1
}
} else {
// 主持人不知道是否为空箱,只打开B箱,如果奖品在B中,则失败,不在B中则切换
if (!rewards[B]) {
checked = rewards[C]
counts.change += 1
}
}
}
if (checked) {
counts.hit += 1
}
}
console.log('抽奖次数:', counts.max)
console.log('是否交换:', config.isExchange ? '交换' : '不交换')
console.log('是否之情:', config.isKnow ? '主持人明确知道B或C中的空箱' : '主持人被蒙在鼓里')
console.log('更换次数:', counts.change)
console.log('中奖次数:', counts.hit)
console.log('获奖概率:', (counts.hit / counts.max * 100).toFixed(2) + '%')
console.log()
}
function random (max) {
return Math.floor(Math.random() * max)
}
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = random(currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment