Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
38
Task/Monty-Hall-problem/JavaScript/monty-hall-problem-1.js
Normal file
38
Task/Monty-Hall-problem/JavaScript/monty-hall-problem-1.js
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
function montyhall(tests, doors) {
|
||||
'use strict';
|
||||
tests = tests ? tests : 1000;
|
||||
doors = doors ? doors : 3;
|
||||
var prizeDoor, chosenDoor, shownDoor, switchDoor, chosenWins = 0, switchWins = 0;
|
||||
|
||||
// randomly pick a door excluding input doors
|
||||
function pick(excludeA, excludeB) {
|
||||
var door;
|
||||
do {
|
||||
door = Math.floor(Math.random() * doors);
|
||||
} while (door === excludeA || door === excludeB);
|
||||
return door;
|
||||
}
|
||||
|
||||
// run tests
|
||||
for (var i = 0; i < tests; i ++) {
|
||||
|
||||
// pick set of doors
|
||||
prizeDoor = pick();
|
||||
chosenDoor = pick();
|
||||
shownDoor = pick(prizeDoor, chosenDoor);
|
||||
switchDoor = pick(chosenDoor, shownDoor);
|
||||
|
||||
// test set for both choices
|
||||
if (chosenDoor === prizeDoor) {
|
||||
chosenWins ++;
|
||||
} else if (switchDoor === prizeDoor) {
|
||||
switchWins ++;
|
||||
}
|
||||
}
|
||||
|
||||
// results
|
||||
return {
|
||||
stayWins: chosenWins + ' ' + (100 * chosenWins / tests) + '%',
|
||||
switchWins: switchWins + ' ' + (100 * switchWins / tests) + '%'
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
montyhall(1000, 3)
|
||||
Object {stayWins: "349 34.9%", switchWins: "651 65.1%"}
|
||||
montyhall(1000, 4)
|
||||
Object {stayWins: "253 25.3%", switchWins: "384 38.4%"}
|
||||
montyhall(1000, 5)
|
||||
Object {stayWins: "202 20.2%", switchWins: "265 26.5%"}
|
||||
49
Task/Monty-Hall-problem/JavaScript/monty-hall-problem-3.js
Normal file
49
Task/Monty-Hall-problem/JavaScript/monty-hall-problem-3.js
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
<html>
|
||||
|
||||
<body>
|
||||
|
||||
<input id="userInputMH" value="1000">
|
||||
<input id="userInputDoor" value="3">
|
||||
<br>
|
||||
<button onclick="montyhall()">Calculate</button>
|
||||
<p id="firstPickWins"></p>
|
||||
<p id="switchPickWins"></p>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
||||
|
||||
<script>
|
||||
function montyhall() {
|
||||
var tests = document.getElementById("userInputMH").value;
|
||||
var doors = document.getElementById("userInputDoor").value;
|
||||
var prizeDoor, chosenDoor, shownDoor, switchDoor, chosenWins = 0,switchWins = 0;
|
||||
|
||||
function pick(excludeA, excludeB) {
|
||||
var door;
|
||||
do {
|
||||
door = Math.floor(Math.random() * doors);
|
||||
} while (door === excludeA || door === excludeB);
|
||||
return door;
|
||||
}
|
||||
|
||||
|
||||
for (var i = 0; i < tests; i++) {
|
||||
|
||||
prizeDoor = pick();
|
||||
chosenDoor = pick();
|
||||
shownDoor = pick(prizeDoor, chosenDoor);
|
||||
switchDoor = pick(chosenDoor, shownDoor);
|
||||
|
||||
if (chosenDoor === prizeDoor) {
|
||||
chosenWins++;
|
||||
} else if (switchDoor === prizeDoor) {
|
||||
switchWins++;
|
||||
}
|
||||
}
|
||||
document.getElementById("firstPickWins").innerHTML = 'First Door Wins: ' + chosenWins + ' | ' + (100 * chosenWins / tests) + '%';
|
||||
document.getElementById("switchPickWins").innerHTML = 'Switched Door Wins: ' + switchWins + ' | ' + (100 * switchWins / tests) + '%';
|
||||
}
|
||||
|
||||
</script>
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(1000, 3)
|
||||
First Door Wins: 346 | 34.6%
|
||||
Switching Door Wins: 654 | 65.4%
|
||||
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));
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
Playing 10000 games
|
||||
Wins when not switching door 3326
|
||||
Wins when switching door 6630
|
||||
Loading…
Add table
Add a link
Reference in a new issue