import math
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import StrMethodFormatter as SMF
import numpy as np
import pandas as pd
pd.set_option('display.precision', 2)
from random import shuffle as RS
import seaborn as sns
import time
def getDeck():
deck = []
for m in range(13):
deck.append(str(m))
deck = deck * 4
return(deck)
def collCardG(deck) :
p = len(deck)/2
while len(deck) > 0 :
if deck.pop() == deck.pop() :
deal = p - len(deck)/2
return("Loss", deal)
return("Win", p+0.1)
def monteCarlo(n, t) :
record = []
for i in range(n):
deck = getDeck()
if t == 'ec':
deck *= 2
RS(deck)
record.append(collCardG(deck)[1])
freqDist = {}
for r in record :
if r in freqDist.keys():
freqDist[r] += 1
else :
freqDist[r] = 1
freqDist = dict(sorted(freqDist.items()))
df = pd.DataFrame(data = {"Round": freqDist.keys(),
"Frequency": freqDist.values(),
"Outcome": ["Loss"]*(len(freqDist)-1)+["Win"]})
df.loc[len(df)-1, 'Round'] -= 0.1
return(df)
def barHelper(df, n, t) :
df['Percent'] = round(100.00*df['Frequency']/df['Frequency'].sum(), 1).astype(str) + '%'
pad = df.loc[len(df)-2, 'Frequency']
if t == 'fiddler' :
d = 1
xmax = 26
ymax = 0.25 * n
title = ' (1 Deck)'
x1 = 0.5
y1 = n/1000
x2 = 0.88*xmax
y2 = n/8
else :
d = 2
xmax = 52
ymax = 0.075 * n
title = ' (2 Decks)'
x1 = 0.9
y1 = n/2000
x2 = 0.91*xmax
y2 = n/80
sns.set()
fig = plt.figure(figsize = (1
fig.savefig("2024.04.20" + t + ".png", bbox_inches = 'tight')
start_time = time.time()
df = monteCarlo(1000000000, 'fiddler')
print(df)
print(time.time() - start_time)
barHelper(df, 1000000000, 'fiddler')
Round Frequency Outcome 0 1.0 58826949 Loss 1 2.0 55327340 Loss 2 3.0 52028581 Loss 3 4.0 48930957 Loss 4 5.0 46023654 Loss 5 6.0 43295451 Loss 6 7.0 40713412 Loss 7 8.0 38301018 Loss 8 9.0 36027836 Loss 9 10.0 33889430 Loss 10 11.0 31887613 Loss 11 12.0 30012269 Loss 12 13.0 28231474 Loss 13 14.0 26560500 Loss 14 15.0 25007064 Loss 15 16.0 23522135 Loss 16 17.0 22137058 Loss 17 18.0 20836691 Loss 18 19.0 19615296 Loss 19 20.0 18465453 Loss 20 21.0 17377988 Loss 21 22.0 16367205 Loss 22 23.0 15402573 Loss 23 24.0 14499283 Loss 24 25.0 13652776 Loss 25 26.0 12856677 Loss 26 26.0 210203317 Win 19522.50635576248
start_time = time.time()
df2 = monteCarlo(1000000000, 'ec')
print(df2)
print(time.time() - start_time)
barHelper(df2, 1000000000, 'ec')
Round Frequency Outcome 0 1.0 67965157 Loss 1 2.0 63343445 Loss 2 3.0 59007147 Loss 3 4.0 54981412 Loss 4 5.0 51261835 Loss 5 6.0 47759221 Loss 6 7.0 44521266 Loss 7 8.0 41468445 Loss 8 9.0 38654442 Loss 9 10.0 36029327 Loss 10 11.0 33576942 Loss 11 12.0 31290397 Loss 12 13.0 29167309 Loss 13 14.0 27172757 Loss 14 15.0 25333365 Loss 15 16.0 23613007 Loss 16 17.0 22013277 Loss 17 18.0 20514757 Loss 18 19.0 19125547 Loss 19 20.0 17818223 Loss 20 21.0 16612738 Loss 21 22.0 15481175 Loss 22 23.0 14427718 Loss 23 24.0 13451323 Loss 24 25.0 12537282 Loss 25 26.0 11685440 Loss 26 27.0 10899545 Loss 27 28.0 10152410 Loss 28 29.0 9463953 Loss 29 30.0 8824934 Loss 30 31.0 8233891 Loss 31 32.0 7673357 Loss 32 33.0 7155056 Loss 33 34.0 6670221 Loss 34 35.0 6215454 Loss 35 36.0 5794870 Loss 36 37.0 5405290 Loss 37 38.0 5038693 Loss 38 39.0 4698832 Loss 39 40.0 4382015 Loss 40 41.0 4083250 Loss 41 42.0 3808199 Loss 42 43.0 3551525 Loss 43 44.0 3310298 Loss 44 45.0 3087566 Loss 45 46.0 2880665 Loss 46 47.0 2687162 Loss 47 48.0 2504066 Loss 48 49.0 2330862 Loss 49 50.0 2173025 Loss 50 51.0 2026110 Loss 51 52.0 1892437 Loss 52 52.0 26243360 Win 35273.89794301987