# Calculate Poker probabilities in Python

When we talk about Poker, we require to analyze the world of shuffled decks. Basically it belongs to the discrete probability domain.

## Discussing shuffle, permutation, and combination:

- Shuffle: Shuffle over any set is calculated using factorial. Factorial is the product of N consecutive positive integers.
for example, 5! = 1*2*3*4*5 = 120. Note: N! = N*(N-1)!

- Permutation: It means shuffling a fixed deck of cards and taking a certain number of cards from it.
- Suppose after shuffling N cards we take R cards. Then it gives us the formula for permutation as follows, NpR = N!/(N-R)! = N*(N-1)*(N-2)*….*(N-R+1) ,
**(note: R<=N)** - Combination: Basically calculation of permutation leads us to the combination! It is the permutation of that portion for which we ignore shuffling.

The formula for combination is NcR = NpR/R!

#Python implementation for permutation and combinatiion shufflings = lambda n: reduce(operator.mul,range(1,n+1)) permutations = lambda n, r: reduce(operator.mul,range(n-r+1,n+1)) combinations = lambda n, r: permutations(n,r)/shufflings(r)

All poker games follow a basic construct as discussed below. Each card has different qualities.

So while approaching you should follow following constructive steps:

- See the type of the game and the nature of the world that you have to analyze.
- You need to ask questions to yourself. And decide your choice permutation, combination, etc.
- Then we need to identify disjoint and overlapping scenarios.
- Finally, we need to implement all this through python function. In the code shown below, we have 10 possible sequences for the cards and as it is obvious we have 13 cards for a color.

import math def combinations(n,k): all_posibilities = float(math.factorial(n) / (math.factorial(k) * math.factorial(n - k))) return all_posibilities def calculate_probability(frequency): all_posibilities = combinations(52,5) return (frequency / all_posibilities) * 100

def poker_probabilities(): royal_flush_frequency = combinations(4,1) royal_flush_probability = calculate_probability(royal_flush_frequency) straight_flush_frequency = combinations(4,1) * combinations(9,1) straight_flush_probability = calculate_probability(straight_flush_frequency) four_of_a_kind_frequency = combinations(13,1) * combinations(13-1,1) * combinations(4,1) #Available 13 cards, also 12 possibilities for the fifth one and 4 colors four_of_a_kind_probability = calculate_probability(four_of_a_kind_frequency) full_house_frequency = combinations(13,1) * combinations(4,3) * combinations(13-1,1) * combinations(4,2) #We have first three: 13 cards, 4 posibilities, last two: 12 cards, 6 posibilities full_house_probability = calculate_probability(full_house_frequency) flush_frequency = (combinations(13,5) * combinations(4,1) - royal_flush_frequency - straight_flush_frequency) flush = calculate_probability(flush_frequency) straight_frequency = combinations(10,1) * 4**5 - straight_flush_frequency # 10 possible sequences are there,and also 4 choices from all the colours straight_probability = calculate_probability(straight_frequency) three_of_a_kind_frequency = combinations(13,1) * combinations(4,3) * combinations(13-1,2) * 4**2 # Available 13 cards, 4 posibilities,we need to choose 2 from 12 cards, three_of_a_kind_probability = calculate_probability(three_of_a_kind_frequency) two_pair_frequency = combinations(13,2) * combinations(4,2)**2 * combinations(13-2,1) * combinations(4,1) # 2 pairs and the fifth card not from a pair two_pair_probability = calculate_probability(two_pair_frequency) one_pair_frequency = combinations(13,1) * combinations(4,2) * combinations(13-1,3)* combinations(4,1)**3 # 1 pair and three random cards without the one in the pair one_pair_probability = calculate_probability(one_pair_frequency) no_pair_frequency = (combinations(13,5) - 10) * (combinations(4,1)**5-4) # no pair no_pair_probability = calculate_probability(no_pair_frequency) print(royal_flush_probability) poker_probabilities()

Output: 0.000153907716932927

## Leave a Reply