diff --git a/dices.js b/dices.js index a43ffea..1a7879b 100644 --- a/dices.js +++ b/dices.js @@ -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. - \ No newline at end of file + console.log(lancerDes(100, 6)); // Remplacez les valeurs par le nombre de dés et de faces que vous souhaitez utiliser. diff --git a/monty_hall.js b/monty_hall.js new file mode 100644 index 0000000..7dc8d19 --- /dev/null +++ b/monty_hall.js @@ -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 \ No newline at end of file diff --git a/monty_hall_Manon.js b/monty_hall_Manon.js new file mode 100644 index 0000000..833d301 --- /dev/null +++ b/monty_hall_Manon.js @@ -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.` + ); \ No newline at end of file