Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
44
Task/Monty-Hall-problem/JavaScript/monty-hall-problem-5.js
Normal file
44
Task/Monty-Hall-problem/JavaScript/monty-hall-problem-5.js
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
var totalGames = 10000,
|
||||
selectDoor = function () {
|
||||
return Math.floor(Math.random() * 3); // Choose a number from 0, 1 and 2.
|
||||
},
|
||||
games = (function () {
|
||||
var i = 0, games = [];
|
||||
|
||||
for (; i < totalGames; ++i) {
|
||||
games.push(selectDoor()); // Pick a door which will hide the prize.
|
||||
}
|
||||
|
||||
return games;
|
||||
}()),
|
||||
play = function (switchDoor) {
|
||||
var i = 0, j = games.length, winningDoor, randomGuess, totalTimesWon = 0;
|
||||
|
||||
for (; i < j; ++i) {
|
||||
winningDoor = games[i];
|
||||
randomGuess = selectDoor();
|
||||
if ((randomGuess === winningDoor && !switchDoor) ||
|
||||
(randomGuess !== winningDoor && switchDoor))
|
||||
{
|
||||
/*
|
||||
* If I initially guessed the winning door and didn't switch,
|
||||
* or if I initially guessed a losing door but then switched,
|
||||
* I've won.
|
||||
*
|
||||
* I lose when I initially guess the winning door and then switch,
|
||||
* or initially guess a losing door and don't switch.
|
||||
*/
|
||||
|
||||
totalTimesWon++;
|
||||
}
|
||||
}
|
||||
return totalTimesWon;
|
||||
};
|
||||
|
||||
/*
|
||||
* Start the simulation
|
||||
*/
|
||||
|
||||
console.log("Playing " + totalGames + " games");
|
||||
console.log("Wins when not switching door", play(false));
|
||||
console.log("Wins when switching door", play(true));
|
||||
Loading…
Add table
Add a link
Reference in a new issue