Advertisement
Guest User

Untitled

a guest
Nov 21st, 2021
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. (function () {
  2. function mh_case(strategy) {
  3. const prizeDoorNr = Math.floor(Math.random() * 3 + 1);
  4. const goatDoorNrs = [1, 2, 3].filter(x => x !== prizeDoorNr);
  5. const initialPlayerChoice = Math.floor(Math.random() * 3 + 1);
  6. const playerChosePrize = prizeDoorNr === initialPlayerChoice;
  7. const hostRevealsDoorNr = playerChosePrize ? (Math.random() > .5 ? goatDoorNrs[0] : goatDoorNrs[1]) : (goatDoorNrs.find(x => x !== initialPlayerChoice));
  8.  
  9. switch (strategy) {
  10. case 'stay': {
  11. return playerChosePrize;
  12. }
  13. case 'switch': {
  14. const newPlayerChoice = [1, 2, 3].find(x => x !== initialPlayerChoice && x !== hostRevealsDoorNr);
  15. return newPlayerChoice === prizeDoorNr;
  16. }
  17. }
  18.  
  19. }
  20.  
  21. const NUMBER_OF_GAMES = 10000;
  22. let choseGoatCount = choseCarCount = 0;
  23. for (let i = 0; i < NUMBER_OF_GAMES; i++) {
  24. mh_case('stay') ? choseCarCount++ : choseGoatCount++;
  25. }
  26. console.log('stay win%: ', choseCarCount / (choseCarCount + choseGoatCount));
  27.  
  28. choseGoatCount = choseCarCount = 0;
  29. for (let i = 0; i < NUMBER_OF_GAMES; i++) {
  30. mh_case('switch') ? choseCarCount++ : choseGoatCount++;
  31. }
  32. console.log('switch win%: ', choseCarCount / (choseCarCount + choseGoatCount));}()
  33. );
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement