-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpokerSimulation.py
More file actions
67 lines (52 loc) · 1.83 KB
/
Copy pathpokerSimulation.py
File metadata and controls
67 lines (52 loc) · 1.83 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 14 14:47:00 2024
@author: fatimarahimi
"""
import random
from collections import Counter
# Define ranks and suits
ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suits = ['hearts', 'diamonds', 'clubs', 'spades']
# Define the deck of 52 cards
deck = [rank + ' of ' + suit for rank in ranks for suit in suits]
# Function to deal a hand
def deal_hand():
deck_copy = deck.copy()
random.shuffle(deck_copy)
# Deal two hole cards to the player
hole_cards = deck_copy[:2]
# Deal five community cards (flop, turn, river)
community_cards = deck_copy[2:7]
return hole_cards, community_cards
# Helper function to extract ranks from cards
def extract_ranks(cards):
return [card.split(' ')[0] for card in cards]
# Function to evaluate the hand
def evaluate_hand(hole_cards, community_cards):
all_cards = hole_cards + community_cards
ranks = extract_ranks(all_cards)
rank_count = Counter(ranks)
if 2 in rank_count.values():
return 'Pair'
elif 3 in rank_count.values():
return 'Three of a Kind'
elif 4 in rank_count.values():
return 'Four of a Kind'
else:
return 'High Card'
# Monte Carlo simulation for poker hands
def poker_sim(num_simulations):
hand_frequencies = Counter()
for _ in range(num_simulations):
hole_cards, community_cards = deal_hand()
hand_result = evaluate_hand(hole_cards, community_cards)
hand_frequencies[hand_result] += 1
return hand_frequencies
# Run the simulation for 10000 games
num_simulations = 10000
simulation_results = poker_sim(num_simulations)
# Print the results
for hand, frequency in simulation_results.items():
print(f'{hand}: {frequency} times ({(frequency / num_simulations) * 100:.2f}%)')