A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 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) + '%'
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue