Skip to content

Instantly share code, notes, and snippets.

@ataylor284
Created February 23, 2016 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ataylor284/cdafcf2e27ada468880c to your computer and use it in GitHub Desktop.
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.
// 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