-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.html
175 lines (160 loc) · 6.95 KB
/
code.html
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html>
<html>
<head>
<title>Monty Hall Simulation</title>
<meta charset="utf-8" />
<style>
header {
background-color: white;
height: 80px;
padding-top: 5px;
}
#wrapper {
margin: 0 auto;
width: 90%;
text-align: center;
}
#left {
width: 33%;
float: left;
background-color: steelblue;
height: 400px;
}
#middle {
width: 34%;
float: left;
background-color: yellow;
height: 400px;
}
#right {
width: 33%;
float: right;
background-color: palevioletred;
height: 400px;
}
footer {
background-color: tan;
}
</style>
</head>
<body>
<div id="wrapper">
<header><h1>Monty Hall Simulation</h1></header>
<main>
<div id="left">
<h1>Issue</h1>
<p>
The Monty Hall problem is named for
its similarity to the Let's Make a Deal television game show
hosted by Monty Hall. The problem is stated as follows.
Assume that a room is equipped with three doors.
Behind two are goats, and behind the third is a shiny new car.
You are asked to pick a door, and will win whatever is behind it.
Let's say you pick door 1. Before the door is opened,
however, someone who knows what's behind the doors (Monty Hall)
opens one of the other two doors, revealing a goat, and asks you if
you wish to change your selection to the third door
(i.e., the door which neither you picked nor he opened).
The Monty Hall problem is deciding whether you do.
</p>
</div>
<div id="middle">
<h1>Simulation</h1>
<script>
//random number 1,2,3
function randomizer() {
var randomNum = Math.floor(Math.random() * 3 + 1);
return randomNum;
}
//random number 0 or 1
function random01() {
var test = Math.round(Math.random());
if (test === 0) {
return true;
}
else {
return false;
}
}
//MAIN: simulation function
function montyHallSim() {
totalLoopsRan = parseInt(prompt("How many loops do you want to run?"));
switchAndCorrect = 0;
noSwitchAndCorrect = 0;
loopCount = 0;
//iterate simulation 1000x
for (i = 0; i < totalLoopsRan; i++) {
//count loop iteration
loopCount++;
boxArray = [1, 2, 3];
winningBox = randomizer(); //number of winning box
//user choice
userOriginalChoice = randomizer();
//user chooses the winning box: stay to win
if (userOriginalChoice == winningBox) {
//remove winning box from takeaway_array
boxArray.splice(boxArray.indexOf(winningBox), 1);
//randomly remove 1 box
takeAwayBoxBool = random01();
if (takeAwayBoxBool) {
takeAwayBox = boxArray[0];
}
else {
takeAwayBox = boxArray[1];
}
//remove takeaway box
boxArray.splice(boxArray.indexOf(takeAwayBox), 1);
//stay to win
noSwitchAndCorrect++;
}
//user choice is not the winning box: switch to win
else {
//randomly takeaway one of the remaining ones
takeAwayBoxBool = random01();
if (takeAwayBoxBool) {
takeAwayBox = boxArray[0];
}
else {
takeAwayBox = boxArray[1];
}
//remove takeaway box
boxArray.splice(boxArray.indexOf(takeAwayBox), 1);
switchAndCorrect++;
}
}
//displaying result
var resultOutput = "Switched and won: " + switchAndCorrect + "<br />" + "No switch and won: " + noSwitchAndCorrect;
resultOutput += "<br />" + "Total times loop ran: " + loopCount;
document.getElementById("pResult").innerHTML = resultOutput;
}
</script>
<img src="/images/lets-make-a-deal.jpg" alt="Let's Make a Deal!" style="width:50%; height:50%"/>
<p id="pResult">
<a href="#"
onclick="javascript: montyHallSim();">
Begin Simulation
</a>
</p>
</div>
<div id="right">
<h1>Solution</h1>
<p>
The correct answer is that you do want to switch.
If you do not switch, you have the expected 1/3 chance
of winning the car, since no matter whether you initially
picked the correct door, Monty will show you a door with a
goat. But after Monty has eliminated one of the doors
for you, you obviously do not improve your chances of
winning to better than 1/3 by sticking with your original
choice. If you now switch doors, however, there is a 2/3
chance you will win the car (counterintuitive though it seems).
</p>
</div>
</main>
<footer>
<h2>Source for Issue and Solution Column: http://mathworld.wolfram.com/MontyHallProblem.html</h2>
<h2>Questions? Contact zhaolin.tong@baruchmail.cuny.edu</h2>
</footer>
</div>
</body>
</html>