Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- function mh_case(strategy) {
- const prizeDoorNr = Math.floor(Math.random() * 3 + 1);
- const goatDoorNrs = [1, 2, 3].filter(x => x !== prizeDoorNr);
- const initialPlayerChoice = Math.floor(Math.random() * 3 + 1);
- const playerChosePrize = prizeDoorNr === initialPlayerChoice;
- const hostRevealsDoorNr = playerChosePrize ? (Math.random() > .5 ? goatDoorNrs[0] : goatDoorNrs[1]) : (goatDoorNrs.find(x => x !== initialPlayerChoice));
- switch (strategy) {
- case 'stay': {
- return playerChosePrize;
- }
- case 'switch': {
- const newPlayerChoice = [1, 2, 3].find(x => x !== initialPlayerChoice && x !== hostRevealsDoorNr);
- return newPlayerChoice === prizeDoorNr;
- }
- }
- }
- const NUMBER_OF_GAMES = 10000;
- let choseGoatCount = choseCarCount = 0;
- for (let i = 0; i < NUMBER_OF_GAMES; i++) {
- mh_case('stay') ? choseCarCount++ : choseGoatCount++;
- }
- console.log('stay win%: ', choseCarCount / (choseCarCount + choseGoatCount));
- choseGoatCount = choseCarCount = 0;
- for (let i = 0; i < NUMBER_OF_GAMES; i++) {
- mh_case('switch') ? choseCarCount++ : choseGoatCount++;
- }
- console.log('switch win%: ', choseCarCount / (choseCarCount + choseGoatCount));}()
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement