-
Notifications
You must be signed in to change notification settings - Fork 0
/
straight_hand.py
40 lines (33 loc) · 1.4 KB
/
straight_hand.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
Calculate the probability of a straight hand in a game of poker.
See https://en.wikipedia.org/wiki/List_of_poker_hands#Straight for more details
"""
import matplotlib
import random
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
def cards():
n = 10000
estimates = []
deck = list(range(13))*4 # Ranks represented as numbers 0-12. 4 of each rank in deck
straights = [range(5), range(1, 6), range(2, 7), range(3, 8), range(4, 9),
range(5, 10), range(6, 11), range(7, 12), range(8, 13), list(range(4)) + [12]]
# 9 different ways to have a straight
# E.g. range (5) is 2-3-4-5-6
# range(4) + [12] is A-2-3-4-5, but listed as 2-3-4-5-A
count_straights = 0 # record the number of observed straights
for i in range(n):
hand = random.sample(deck, 5) # get a random sample of 5 elements from the deck
hand = sorted(hand) # sort the cards in ascending order
if hand in straights: # check to see if this hand is a straight
count_straights += 1
estimates.append(count_straights / (float(i+1)))
# the formula is estimate = (num observations / num trials)
plt.figure(1)
plt.semilogx(estimates)
plt.xlabel("log(number of samples)")
plt.ylabel("estimated probability")
plt.title("Estimating the Probability of a Poker Straight")
plt.show()
if __name__ == "__main__":
cards()