Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created May 8, 2024 10:07
Show Gist options
  • Save abikoushi/0d6a95a3b370ceca6b3ed7b8c683da42 to your computer and use it in GitHub Desktop.
Save abikoushi/0d6a95a3b370ceca6b3ed7b8c683da42 to your computer and use it in GitHub Desktop.
Monte-Carlo simulation of the five cards
drawfive <- function(iter){
three = 0L
four = 0L
onepair = 0L
twopair = 0L
fullhouse = 0L
straight = 0L
flush = 0L
#iter <- 100000
dec <- expand.grid(suit=1L:4L,rank=1L:13L)
for(i in 1:iter){
ind <- sort(sample.int(52L, size = 5))
hand <- dec[ind,]
flush = flush + as.integer(any(table(hand$suit)==5L))
straight = straight + as.integer(all(diff(sort(hand$rank))==1L))
tabr <- table(hand$rank)
fullhouse = fullhouse + as.integer(any(tabr==2L)&any(tabr==3L))
four = four + as.integer(any(tabr==4L))
three = three + as.integer(any(tabr==3L))
onepair = onepair + as.integer(any(tabr==2L))
twopair = twopair + as.integer(sum(tabr==2L)==2L)
}
c(straight = straight,
flush = flush,
fullhouse = fullhouse,
four=four,
three=three,
twopair=twopair,
onepair=onepair)/iter
}
set.seed(12345)
system.time({
res <- drawfive(100000)
})
round(res, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment