Adding Monty Hall problem

This commit is contained in:
gauvainboiche
2025-07-23 16:32:12 +02:00
parent 1026e526c9
commit d46e294ff3
3 changed files with 70 additions and 2 deletions

View File

@@ -18,5 +18,4 @@ function lancerDes(nombreDes, nombreFaces) {
}
// Utilisation
console.log(lancerDes(1, 7)); // Remplacez les valeurs par le nombre de dés et de faces que vous souhaitez utiliser.
console.log(lancerDes(100, 6)); // Remplacez les valeurs par le nombre de dés et de faces que vous souhaitez utiliser.

21
monty_hall.js Normal file
View File

@@ -0,0 +1,21 @@
const choices = {
choice0: false,
choice1: false,
choice2: false
};
const getRandomChoice = (max) => {
return Math.floor(Math.random() * max);
}
const setTrue = () => {
let randValue = getRandomChoice(3);
let key = `choice${randValue}`
choices[key] = true;
}
setTrue();
// let userChoice = prompt("Quelle porte choisissez-vous ? 1, 2 ou 3 ? ", 2);
if

48
monty_hall_Manon.js Normal file
View File

@@ -0,0 +1,48 @@
function percentage(partialValue, totalValue) {
return Math.round((100 * partialValue) / totalValue);
}
function rand(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const ONE_THOUSAND = 1000;
const TEN_THOUSAND = ONE_THOUSAND * 10;
const ONE_HUNDRED_THOUSAND = TEN_THOUSAND * 10;
const ONE_MILLION = ONE_HUNDRED_THOUSAND * 10;
const numberOfDoors = 3;
const numberOfTries = 10000;
let originalDoorWins = 0; // la porte gagnante
let anotherDoorWins = 0; // si on change de porte
for (let i = 0; i < numberOfTries; i++) {
const myDoor = rand(1, numberOfDoors); // celle que je choisis
const correctDoor = rand(1, numberOfDoors);
if (myDoor === correctDoor) {
originalDoorWins++;
} else {
anotherDoorWins++;
}
//console.log({myDoor, correctDoor});
}
console.log(`For ${numberOfTries} tries:`);
console.log(
`${originalDoorWins} wins (${percentage(
originalDoorWins,
numberOfTries
)}%) when keeping the original door.`
);
console.log(
`${anotherDoorWins} wins (${percentage(
anotherDoorWins,
numberOfTries
)}%) when picking another door.`
);