Created
February 23, 2016 20:55
-
-
Save ataylor284/cdafcf2e27ada468880c to your computer and use it in GitHub Desktop.
Calculate odds to draw at least one card in target set from a deck with n draws.
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
// calculate odds to draw at least one card in target set from a deck | |
// with n draws | |
// total size | |
def deckSize = 52 | |
// cards already consumed | |
def burned = 0 | |
// number of copies of card we're looking for in the deck | |
def targets = 4 | |
// number of draws we have | |
def draws = 1 | |
// number of redraws we have | |
//def mulligans = 0 FIXME | |
// figure out the odds | |
def cardsLeft = deckSize - burned | |
def negProb = 1.0 | |
def p = 1.0 | |
draws.times { | |
def n = ((cardsLeft - targets) / cardsLeft) | |
def y = 1.0 - n | |
//printf "${cardsLeft - targets} / $cardsLeft: %.02f%% %.02f%%\n", n * 100.0, y * 100.0 | |
negProb -= y * p | |
p = p * n | |
cardsLeft -= 1 | |
} | |
printf "%.02f%%\n", (1.0 - negProb) * 100.0 | |
// sanity check: simulate it | |
def hits = 0 | |
10000.times { | |
def deck = (0..<deckSize).toList()[0..-(burned + 1)] | |
Collections.shuffle(deck) | |
def drawn = deck.take(draws) | |
if (drawn.any { (0..<targets).contains(it) }) { | |
hits++ | |
} | |
} | |
printf "%.02f%%\n", new BigDecimal(hits) / 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment