Created
March 8, 2020 15:17
-
-
Save DannyDannyDanny/b03db7bdd12fe69fe04a1ad7228fc49e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random as r | |
import matplotlib.pyplot as plt | |
# Generate rolls | |
n_rolls = 1000000 | |
rolls = [(r.randint(1,6),r.randint(1,6)) for i in range(n_rolls)] | |
flat_list = [item for sublist in rolls for item in sublist] | |
plt.hist(flat_list,bins=12) | |
plt.show() | |
# %% | |
hands = """41 | |
42 | |
43 | |
51 | |
52 | |
53 | |
54 | |
61 | |
62 | |
63 | |
64 | |
65 | |
11 | |
22 | |
33 | |
44 | |
55 | |
66 | |
13 | |
12 | |
32""" | |
hands = {h:i for i,h in enumerate(hands.split('\n'))} | |
# %% | |
def get_rank(roll): | |
roll = ''.join([str(i) for i in roll]) | |
rolr = ''.join([str(i) for i in reversed(roll)]) | |
rank = -1 | |
try: | |
rank = hands[roll] | |
# print(roll) | |
except KeyError: | |
rank = hands[rolr] | |
# print(rolr) | |
# print(rank) | |
return rank | |
ranks = [get_rank(r) for r in rolls] | |
# %% | |
import numpy as np | |
objects = [k for k,v in hands.items()] | |
y_pos = np.arange(len(objects)) | |
occurences = [ranks.count(i) for i in hands.values()] | |
occurences = [occ/n_rolls for occ in occurences] | |
plt.figure(figsize=(10,6)) | |
plt.bar(y_pos, occurences, align='center', alpha=0.9) | |
plt.xticks(y_pos, objects) | |
plt.ylabel('Probability') | |
plt.xlabel('Mejer "hand"') | |
plt.title(f'Mejer hands rolled in {n_rolls} rolls') | |
plt.show() | |
# %% | |
cumulative = [sum(occurences[:i+1]) for i in range(len(occurences))] | |
objects = [k for k,v in hands.items()] | |
y_pos = np.arange(len(objects)) | |
[(list(hands.keys())[i],round(100*(1 - c))) for i,c in enumerate(cumulative)] | |
plt.figure(figsize=(10,6)) | |
plt.bar(y_pos, cumulative, align='center', alpha=0.9) | |
plt.xticks(y_pos, objects) | |
plt.ylabel('Probability') | |
plt.xlabel('Mejer "hand"') | |
plt.title(f'Cumulative Prodability of hands rolled or something') | |
plt.axhline(y=0.5,linewidth=1, color='r', label = '50%') | |
plt.show() | |
# %% | |
neg_cumulative = [1-sum(occurences[:i]) for i in range(len(occurences))] | |
fig = plt.figure(figsize=(10,6)) | |
ax = fig.gca() | |
plt.bar(y_pos, neg_cumulative, align='center', alpha=0.95) | |
plt.xticks(y_pos, objects) | |
plt.ylim([0,1]) | |
plt.ylabel('Probability') | |
plt.xlabel('Mejer "hand"') | |
plt.title(f'Probability of beating or equalling mejer "hands"') | |
# plt.axhline(y=0.5,linewidth=1, color='r', label = '50%') | |
ax.set_yticks(np.arange(0, 1.1, 0.1)) | |
plt.grid() | |
# plt.legend() | |
plt.show() | |
# %% | |
yee = [((r1,r2),int(get_rank(r1)<=get_rank(r2))) for r1,r2 in zip(rolls[:10],rolls[1:11],)] | |
winloss = [int(get_rank(r1)<=get_rank(r2)) for r1,r2 in zip(rolls[:],rolls[1:])] | |
sum(winloss)/len(winloss) | |
for r,res in yee: | |
r1 = ','.join([str(r) for r in r[0]]) | |
r2 = ','.join([str(r) for r in r[1]]) | |
print(r1 , ' - ' ,r2 , res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment