-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
30 lines (25 loc) · 1 KB
/
index.ts
File metadata and controls
30 lines (25 loc) · 1 KB
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
function simulateMontyHall(numTrials: number, shouldSwitch: boolean) {
let wins = 0;
for (let i = 0; i < numTrials; i++) {
const prizeDoor = Math.floor(Math.random() * 3);
let playerChoice = Math.floor(Math.random() * 3);
let openDoor;
do {
openDoor = Math.floor(Math.random() * 3);
} while (openDoor === playerChoice || openDoor === prizeDoor);
if (shouldSwitch) {
let switchDoor;
do {
switchDoor = Math.floor(Math.random() * 3);
} while (switchDoor === playerChoice || switchDoor === openDoor);
playerChoice = switchDoor;
}
if (playerChoice === prizeDoor) {
wins++;
}
}
return (wins / numTrials) * 100;
}
const numTrials = 1000;
console.log(`Se o participante NÃO trocar de porta: ${simulateMontyHall(numTrials, false)}% de vitórias`);
console.log(`Se o participante trocar de porta: ${simulateMontyHall(numTrials, true)}% de vitórias`);