Skip to content

Instantly share code, notes, and snippets.

@john-science
Created September 30, 2016 21:06
Show Gist options
  • Save john-science/3aacc39fab51abd7680bc7a60c12478c to your computer and use it in GitHub Desktop.
Save john-science/3aacc39fab51abd7680bc7a60c12478c to your computer and use it in GitHub Desktop.

Poker, the Ultimate Data Science Test

If you could solve poker with data science, you could make a lot of money any time you want.

Modeling a Deal

If we want to model poker, the first step is to model a single deal of the cards. Now, casinos might deal from more than one deck. But let's start by modeling a single deal from a single 52-card deck:

def sample (n, m):
    s = []
    for j in range(n-m+1, n + 1):
        t = randint(1, j + 1)
        if t not in s:
            s.append(t)
        else:
            s.append(j)
    return s

This solution is called Floyd's Algorithm.

We can run this for different 5-card hands:

>>> sample(52, 5)
[42, 41, 19, 31, 52]
>>> sample(52, 5)
[30, 23, 37, 5, 17]
>>> sample(52, 5)
[44, 2, 17, 21, 30]
>>> sample(52, 5)
[19, 3, 36, 26, 6]
>>> sample(52, 5)
[23, 41, 11, 26, 34]

And if we want to deal 5 cards to two players, we will want to take 10 cards:

>>> sample(52, 10)
[37, 32, 45, 24, 35, 14, 12, 42, 51, 33]
>>> sample(52, 10)
[37, 4, 29, 39, 2, 44, 12, 5, 50, 53]
>>> sample(52, 10)
[25, 3, 40, 14, 23, 37, 35, 18, 50, 32]
>>> sample(52, 10)
[41, 21, 33, 46, 47, 9, 8, 14, 42, 16]

Next we will want to do two more things:

  1. Associate each card in the deck with one of these numbers.
  2. Randomize the order of the selected cards.

Decide How to Score a Hand

Once dealt, we need to be able to score (or at least) sort hands by their value.

Pick a Poker Game

There are lots of kinds of power, and each one will have to be modeled differently. It is probably best to start with the simplest game, and build up from there.

Five-Card Stud

You are dealt five cards and you can play them or fold.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment