The Monty Hall problem
by chandings
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
<h1>Monty Hall Problem Simulator</h1>
<p>The Monty Hall problem is a brain teaser, in the form of a probability puzzle, loosely based on the American television game show Let's Make a Deal and named after its original host, Monty Hall. The problem was originally posed (and solved) in a letter by Steve Selvin to the American Statistician in 1975. It became famous as a question from a reader's letter quoted in Marilyn vos Savant's "Ask Marilyn" column in Parade magazine in 1990 (vos Savant 1990a):</p>
<p>Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice?</p>
<a href="https://en.wikipedia.org/wiki/Monty_Hall_problem">Wiki</a>
<p>
Below you can choose the total number of doors and the number of doors that Monty Hall opens and run the simulation. The code will run "with switch" and "without switch" smulations separately. The default setting is the same as original game. Have fun.
</p>
<hr>
<div ng-app="app" ng-controller="myController">
<div >
<label>Total Doors:</label><input type="number" ng-change="updatedTotalDoors()" ng-model="totalDoors" min="3" max="100"><br>
<label>Doors Opened:</label><input type="number" ng-model="totalDoorsToOpen" min="1" max="{{maximumDoorOpened}}"><br>
<label>Total Simulations:</label><input type="number" ng-model="totalSimulationsToRun" step="10" min="10" max="100000">
<button ng-disabled="simulationRunning" ng-click="clickHandler()">Run Simulation</button>
<br>
<br>
</div>
<div ng-if="totalSimulationsRan !== 0">
Total Sumulation:{{totalSimulationsRan}}<br>
Total won with door switch:{{wonWithSwitch}} times,...
JavaScript
angular.module("app",[])
.controller("myController",function($scope, utility, $timeout){
var doors = [];
var totalDoors = 3;
var prizeDoorIndex;
var chosenDoorIndex;
var totalDoorsToOpen = 1;
var doorsToOpenIndex = [];
var indexOfOpenedDoors;
var stopSimulation = false;
$scope.totalSimulationsRan = 0;
$scope.wonWithSwitch = 0;
$scope.wonWithoutSwitch = 0;
$scope.totalSimulationsToRun = 10000;
$scope.simulationRunning = false;
$scope.maximumDoorOpened = 1;
$scope.totalDoors = 3;
$scope.totalDoorsToOpen = 1;
$scope.stopRunningSimulation = function(){
stopSimulation = true;
}
$scope.updatedTotalDoors = function(){
$scope.maximumDoorOpened = $scope.totalDoors - 2;
$scope.totalDoorsToOpen = $scope.maximumDoorOpened;
}
$scope.clickHandler = function(){
$scope.simulationRunning = true;
totalDoors = $scope.totalDoors;
totalDoorsToOpen = $scope.totalDoorsToOpen;
$scope.totalSimulationsRan = 0;
$scope.wonWithSwitch = 0;
$scope.wonWithoutSwitch = 0;
stopSimulation = false;
runSimulations();
}
function init(){
doors = [];
totalDoors = $scope.totalDoors;
totalDoorsToOpen = $scope.totalDoorsToOpen;
prizeDoorIndex = -1;
chosenDoorIndex = -1;
doorsToOpenIndex = [];
indexOfOpenedDoors = -1;
}
function setUp(){
prizeDoorIndex = utility.randRange(0, totalDoors-1);
doors = Array.apply(null, Array(totalDoors)).map(() => new Object({isEmpty:true, isOpened:false}));
doors[prizeDoorIndex].isEmpty = false;
}
function chooseDoor(){
chosenDoorIndex = utility.randRange(0, totalDoors-1);
}
function openDoors(){
indexOfOpenedDoors = utility.uniqueRandomNumbers(0, totalDoors-1, totalDoorsToOpen, [prizeDoorIndex,chosenDoorIndex]);
indexOfOpenedDoors.forEach((element)=>{
doors[element].isOpened = true;
});
}
function switchDoors(){
var doorsToSwitchTo = [];
for(let index = 0; index <...