Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function randomChoice(arr){
- return arr[Math.floor(Math.random() * arr.length)];
- }
- function pickAndStay(doors, prize) {
- // Player selects a door.
- let playerSelection = randomChoice(doors);
- // Monty reveals a door you did not select.
- let montyReveals = randomChoice(doors);
- while(montyReveals === playerSelection) {
- montyReveals = randomChoice(doors);
- }
- // If player selected the answer player wins (+1) else loss.
- return playerSelection === prize ? 1 : 0;
- }
- function pickAndSwitch(doors, prize) {
- // Player selects a door.
- let playerSelection = randomChoice(doors);
- // Monty reveals a door you did not select and Monty does not reveal the prize (by coincidence, not by his knowledge).
- let montyReveals = randomChoice(doors);
- while(montyReveals === playerSelection || montyReveals === prize) {
- montyReveals = randomChoice(doors);
- }
- // Player switches, so they get the prize if their first selection is not the answer.
- return playerSelection !== prize ? 1 : 0;
- }
- function trials(n, fn) {
- // Run n trials of strategy defined by function fn and return number of prizes obtained.
- const doors = [0,1,2];
- let total = 0;
- for(let i = 0; i < n; i ++) {
- let prize = randomChoice(doors);
- total += fn(doors, prize);
- }
- return total;
- }
Advertisement
Add Comment
Please, Sign In to add comment