37
loading...
This website collects cookies to deliver better user experience
goat
and one string with the value car
.function getDoorSetup() {
const setup = ["goat", "goat", "car"];
return setup;
}
car
, then we win; if not, we lose. To randomize the order of this array, I will be using a JavaScript library called lodash. lodash is a popular library with a bunch of utility functions that make working with JavaScript data types easier. It has a function called shuffle
that takes an array and returns a new array with the order of the items changed randomly.function getDoorSetup() {
const setup = ["goat", "goat", "car"];
return _.shuffle(setup);
}
getDoorPick
. It will generate a number between 0
and 2
. These numbers will correspond to the indices of the door setup array. We will use a lodash function called random
that returns a random integer in between given numbers.function getDoorPick() {
const doorPick = _.random(0, 2);
return doorPick;
}
playGame
that will simulate playing this game. In this function, we will set up the game and pick a door number. If the door number we picked has the value car
, we win; if not, we lose. We will return 1
from the function if we win, 0
if we lose.function playGame() {
const doorSetup = getDoorSetup();
const doorPick = getDoorPick();
if (doorSetup[doorPick] === "car") {
return 1;
}
return 0;
}
let counter = 0;
let rounds = 100;
let wins = 0;
while (counter < rounds) {
const result = playGame();
wins = wins + result;
counter = counter + 1;
}
console.log(`You have won ${wins} games`);
rounds
variable, which is 100
. This simulation will run 100
times. We are using the wins
variable to count the number of times we win the game. We console.log
the result. Try running the program couple of times. You will notice that the number of times you win will hover around 33. This is 1/3 of 100. The result makes sense since there are 3 options; we have 1 in 3 chances of winning the game.doorSetup
that will not be our pick, and it is not the number with the car either. I will call this function getGoatNotAtDoorNumber
.function getGoatNotAtDoorNumber(setup, doorNumber) {
let goatDoorNumber;
setup.forEach((item, itemNumber) => {
if (item === "goat" && itemNumber !== doorNumber) {
goatDoorNumber = itemNumber;
}
});
return goatDoorNumber;
}
doorSetup
and doorPick
to simulate the host revealing some other door with a goat behind it.const revealedGoatPosition = getGoatNotAtDoorNumber(doorSetup, doorPick);
getFinalPick
. This function will simulate the act of switching or not switching. If we choose not to switch, then we just stick to our original pick. If we switch, we should pick the door number that we didn't pick and the one that isn't revealed. Here is what that function looks like:function getFinalPick(ourPick, revealedPick, changePick) {
if (!changePick) {
return ourPick;
}
const possibleDoorNumbers = [0, 1, 2];
return possibleDoorNumbers.filter((doorNumber) => {
if (doorNumber !== ourPick && doorNumber !== revealedPick) {
return true;
}
})[0];
}
function playGame() {
const setup = getDoorSetup();
const ourDoorPick = getDoorPick();
const revealedGoatPosition = getGoatNotAtDoorNumber(setup, ourDoorPick);
const switchDoors = false;
const ourFinalPick = getFinalPick(
ourDoorPick,
revealedGoatPosition,
switchDoors
);
if (setup[ourFinalPick] === "car") {
return 1;
}
return 0;
}
switchDoors
variable from false
to true
. You will suddenly see that you are winning twice many games on average.1
and 365
to represent a birthday. We can again use lodash to generate the random number.function getRandomBirthday() {
return _.random(1, 365);
}
getRandomBirthdays
to generate an array of the desired length consisting of these random numbers. We can think of this array representing the birthdays of a roomful of people.function getRandomBirthdays(length) {
const randomBirthdays = [];
for (let i = 0; i < length; i++) {
const randomBirthday = getRandomBirthday();
randomBirthdays.push(randomBirthday);
}
return randomBirthdays;
}
function hasSameBirthday(arr) {
const arrWithNoDuplicates = [...new Set(arr)];
if (arrWithNoDuplicates.length !== arr.length) {
return true;
}
return false;
}
const arrWithNoDuplicates = [...new Set(arr)];
true
to indicate there are matching birthdays in the array. If not, we return false
.function simulate() {
const rounds = 100;
let counter = 0;
let matches = 0;
while (counter < rounds) {
const arr = getRandomBirthdays(23);
const hasDuplicate = hasSameBirthday(arr);
if (hasDuplicate) {
matches = matches + 1;
}
counter = counter + 1;
}
console.log(
`There is ${(matches / rounds) * 100}% chance that there is match`
);
}
simulate();