Skip to content

Instantly share code, notes, and snippets.

@renatodex
Created June 21, 2020 21:51
Show Gist options
  • Save renatodex/2a05d741dc2cfadbd4a9848aa2493856 to your computer and use it in GitHub Desktop.
Save renatodex/2a05d741dc2cfadbd4a9848aa2493856 to your computer and use it in GitHub Desktop.
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