Guest User

Untitled

a guest
Jun 9th, 2022
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. function randomChoice(arr){
  2. return arr[Math.floor(Math.random() * arr.length)];
  3. }
  4.  
  5.  
  6. function pickAndStay(doors, prize) {
  7. // Player selects a door.
  8. let playerSelection = randomChoice(doors);
  9.  
  10. // Monty reveals a door you did not select.
  11. let montyReveals = randomChoice(doors);
  12. while(montyReveals === playerSelection) {
  13. montyReveals = randomChoice(doors);
  14. }
  15.  
  16. // If player selected the answer player wins (+1) else loss.
  17. return playerSelection === prize ? 1 : 0;
  18. }
  19.  
  20.  
  21. function pickAndSwitch(doors, prize) {
  22. // Player selects a door.
  23. let playerSelection = randomChoice(doors);
  24.  
  25. // Monty reveals a door you did not select and Monty does not reveal the prize (by coincidence, not by his knowledge).
  26. let montyReveals = randomChoice(doors);
  27. while(montyReveals === playerSelection || montyReveals === prize) {
  28. montyReveals = randomChoice(doors);
  29. }
  30.  
  31. // Player switches, so they get the prize if their first selection is not the answer.
  32. return playerSelection !== prize ? 1 : 0;
  33. }
  34.  
  35. function trials(n, fn) {
  36. // Run n trials of strategy defined by function fn and return number of prizes obtained.
  37. const doors = [0,1,2];
  38. let total = 0;
  39. for(let i = 0; i < n; i ++) {
  40. let prize = randomChoice(doors);
  41. total += fn(doors, prize);
  42. }
  43.  
  44. return total;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment