-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonty_Hall_Sim.js
47 lines (41 loc) · 1.38 KB
/
Monty_Hall_Sim.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//Needs some cleaning up
//The controls for the simulation
let trials = 100000
let switchDoor = true
//Sets a variable to count amount of time correct
let rightAnswers = 0
if (typeof(trials) != "number" || typeof(switchDoor) != "boolean") {
console.log("Please ensure that you have entered a number for trials and true/false for switchDoor")
} else {
for (let i = trials; i > 0; --i) {
//Picks out random doors when called on
const ranDoorPicker = () => {
return Math.floor(Math.random() * 3 + 1);
}
//Sets the door picks for the door and for the persons pick
let computerDoor = ranDoorPicker();
let personDoor = ranDoorPicker();
//Computer picks a door to randomly eliminate
const eliminatedDoorPicker = () => {
let doorTest = ranDoorPicker();
while (doorTest == computerDoor || doorTest == personDoor) {
doorTest = ranDoorPicker();
}
return doorTest;
}
let eliminatedDoor = eliminatedDoorPicker();
//Switching the persons pick
let switchedDoor = 6 - personDoor - eliminatedDoor;
//adds points to overall score
if (switchDoor == true) {
if (switchedDoor == computerDoor) {
++rightAnswers;
}
} else if (switchDoor == false) {
if (personDoor == computerDoor) {
++rightAnswers;
}
}
}
console.log("Percent correct: " + (rightAnswers * 100) / trials + "%")
}