Created
June 21, 2020 21:51
-
-
Save renatodex/2a05d741dc2cfadbd4a9848aa2493856 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Random = require("random-js").Random; | |
function deckProbabilityTest(cycles = 1) { | |
let random = new Random(); | |
let results = [] | |
let originalDeck = [1,2,3,4,5,6] | |
let totalCards = originalDeck.length | |
let desiredCardAtEnd = 5 | |
let firstCardIndex | |
let secondCardIndex | |
let thirdCardIndex | |
let deckAfterFirstDraw | |
let deckAfterSecondDraw | |
for (let x = 0; x < cycles; x++) { | |
// Compra da Primeira Carta | |
firstCardIndex = random.integer(0, totalCards - 1) | |
deckAfterFirstDraw = originalDeck.filter(n => n != firstCardIndex) | |
// Drawing the Second card. | |
secondCardIndex = random.integer(0, deckAfterFirstDraw.length - 1) | |
deckAfterSecondDraw = deckAfterFirstDraw.filter(n => n != secondCardIndex) | |
// Drawing the Third card. | |
thirdCardIndex = random.integer(0, deckAfterSecondDraw.length - 1) | |
// Here we just push the final selected card to an Array. | |
results.push(deckAfterSecondDraw[thirdCardIndex]) | |
} | |
// Retorna a divisão dos resultados bem sucedidos com o número total de testes. | |
return results.filter(r => r == desiredCardAtEnd).length / cycles | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment