RObservations #48: Exploring All Possible Hands in 5 card Poker

[This article was first published on r – bensstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

Recently, I have been reading “Mathematical Statistics” by Professor Keith Knight and I noticed a interesting passage he mentions when discussing finite sample spaces:

*In some cases, it may be possible to enumerate all possible outcomes, but in general such enumeration is physically impossible; for example, enumerating all possible 5 card poker hands dealt from a deck of 52 cards would take several months under the most
favourable conditions. * (Knight 2000)

While this quote is taken out of context, with the advent of modern computing this is a task which is definitely possible to do computationally!

In this blog I show the code I use to list all the possible outcomes of hands dealt in 5 card poker in R!

Setting up the “Card Deck”

To set up the card deck, we need to define our cards (Ace, 1 to 10, Jack, Queen and King) and houses (Hearts, Spades, Diamonds and Clubs). After that, we can create a deck by using two nested for loops.

The code to do this is:

cards <- c(
  "Ace",
  2,
  3,
  4,
  5,
  6,
  7,
  8,
  9,
  10,
  "Jack",
  "Queen",
  "King"
)

houses <- c("Hearts", "Spades","Diamonds","Clubs")

deck <- c()
for(card in cards){
  for(house in houses){
    deck <- append(deck, paste0(card," of ",house))
  }
}

This leaves us with our deck of cards:

deck

 [1] "Ace of Hearts"     "Ace of Spades"     "Ace of Diamonds"   "Ace of Clubs"     
 [5] "2 of Hearts"       "2 of Spades"       "2 of Diamonds"     "2 of Clubs"       
 [9] "3 of Hearts"       "3 of Spades"       "3 of Diamonds"     "3 of Clubs"       
[13] "4 of Hearts"       "4 of Spades"       "4 of Diamonds"     "4 of Clubs"       
[17] "5 of Hearts"       "5 of Spades"       "5 of Diamonds"     "5 of Clubs"       
[21] "6 of Hearts"       "6 of Spades"       "6 of Diamonds"     "6 of Clubs"       
[25] "7 of Hearts"       "7 of Spades"       "7 of Diamonds"     "7 of Clubs"       
[29] "8 of Hearts"       "8 of Spades"       "8 of Diamonds"     "8 of Clubs"       
[33] "9 of Hearts"       "9 of Spades"       "9 of Diamonds"     "9 of Clubs"       
[37] "10 of Hearts"      "10 of Spades"      "10 of Diamonds"    "10 of Clubs"      
[41] "Jack of Hearts"    "Jack of Spades"    "Jack of Diamonds"  "Jack of Clubs"    
[45] "Queen of Hearts"   "Queen of Spades"   "Queen of Diamonds" "Queen of Clubs"   
[49] "King of Hearts"    "King of Spades"    "King of Diamonds"  "King of Clubs"   

With this deck its possible get all the combinations of hands that can be drawn.

Computing all Combinations

To get all possible hands that can be drawn from the deck, we make use of the combn function. With it we can enumerate all possible hands that can be dealt in 5 card poker:

five_card_combinations<- combn(deck, 5)

# get the total number of combinations 

dim(five_card_combinations)[2]

[1] 2598960

Using the combn function it is possible to list all possible 5 hand combinations. For those of you really interested in getting the output I uploaded the data (transposed and with renamed columns) on Kaggle Datasets. If you work on any computational card counting algorithms do let me know so I can check it out!

Conclusion

Contrary to what Knight wrote, it is possible with the power computation to enumerate all combinations of 5 card poker hands dealt. While this is definitely taken out of context, the ability to write code makes things that were once physically impossible to do possible!

I hope you enjoyed this exercise as much as I did!

Thank you for reading!

Want to see more of my content?

Be sure to subscribe and never miss an update!

To leave a comment for the author, please follow the link and comment on their blog: r – bensstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)