Monty Hall JS实现提供50/50输出



显然我的模拟是错误的,但我无法理解为什么。它给出的输出为 50/50,而它应该清楚地是 66/33。我已经查看了 2 天的代码,但仍然找不到它。

我什至查看了一些参考资料,试图了解他们的代码是如何工作的:https://rosettacode.org/wiki/Monty_Hall_problem

我将不胜感激任何帮助,提前感谢。

有一个运行它的 GitHub 页面:https://dupuqub.github.io/testes/monty.html

const roll = (a, b) => Math.floor(Math.random() * (b - a + 1))
const sample = 100000
let yes = 0
let no = 0

for(i = sample; i > 0; i --){

	const doors = ["a","b","c"]

// where the car will be found.
	const car = roll(0,2)

// the fictional player first selected door. 
	const first = roll(0,2)

// the doors that weren't selected.
	const other_doors = doors.filter((door, index) => index !== first)

// the door that is open to show there is no car there.
	const shown = roll(0,1)

// if the player changed or not his mind.
	const change = roll(0,1)

// the player's final decision.
	const final = change ? other_doors[shown ? 0 : 1] : doors[first]

// if the player won or not.
	const winner = doors[car] === final

// if the player WON:
// give a point to YES if there was a change.
// give a point to NO if there wasn't a change.
	if(winner) change ? yes ++ : no ++
}

console.log(sample)
console.log("CHANGED and won: " + yes)
console.log("DID NOT CHANGE and won: " + no)

我调整了你的代码并让它工作。我认为主要错误是打开的门只能是用户不仅没有选择的门,而且是我们知道汽车没有选择的门。

const roll = (a, b) => Math.floor(Math.random() * (b - a + 1))
const sample = 100000
let yes = 0
let no = 0
for(i = sample; i > 0; i --){
const doors = ["a","b","c"]
// the door where the car is
const car = doors[roll(0, 2)]
// the fictional player's first selected door
const first = doors[roll(0, 2)]
// the doors that weren't selected
const other_doors = doors.filter(door => door !== first)
// the doors that can be shown where there is no car
const showable = other_doors.filter(door => door !== car)
// the door that is opened to show there is no car there.
const shown = showable[roll(0, showable.length - 1)]
// the door that was not first selected and was not opened
const closed = doors.filter(door => door !== first && door !== shown)[0];
// the first door the fictional player selected and the remaining closed door.
const final_doors = [first, closed]
// if the player changed or not his mind.
const change = roll(0, 1)
// the player's final decision.
const final = change ? closed : first
// if the player won or not.
const winner = car === final
// if the player WON, give a point to YES if there was a change or to NO if there wasn't a change.
if(winner) change ? yes ++ : no ++
}
console.log(sample)
console.log("CHANGED and won: " + yes)
console.log("DID NOT CHANGE and won: " + no)

最新更新