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', 4)
from random import random as RR
def update(p1, p1s, p2, p2s,
q1, q1s, q2, q2s,
winners, w) :
p1s.append(p1)
p2s.append(p2)
q1s.append(q1)
q2s.append(q2)
winners.append(w)
#Simulation for 2 players
def createSimulation(n) :
p1s = []
p2s = []
q1s = []
q2s = []
winners = []
for i in range(n):
p2 = 0
q1 = 0
q2 = 0
p1 = RR()
#If spin < p, spin again.
if p1 < 0.5:
p2 = RR()
p = p1 + p2
#P1 busts, P2 wins.
if p > 1 :
update(p1, p1s, p2, p2s,
q1, q1s, q2, q2s,
winners, 'P2')
else :
#P2 spin once.
q1 = RR()
#P2 wins on first roll.
if p < q1 :
update(p1, p1s, p2, p2s,
q1, q1s, q2, q2s,
winners, 'P2')
#P2 can spin again.
else :
q2 = RR()
q = q1 + q2
#P1 wins.
if q > 1 or p > q:
update(p1, p1s, p2, p2s,
q1, q1s, q2, q2s,
winners, 'P1')
#P2 wins.
elif p < q:
update(p1, p1s, p2, p2s,
q1, q1s, q2, q2s,
winners, 'P2')
df = pd.DataFrame(data = {'Player 1 Roll 1': p1s,
'Player 1 Roll 2': p2s,
'P1' : [sum(x) for x in zip(p1s, p2s)],
'Player 2 Roll 1': q1s,
'Player 2 Roll 2': q2s,
'P2' : [sum(x) for x in zip(q1s, q2s)],
'Winner': winners})
return(df)
df = createSimulation(100000000)
All
df['Winner'].value_counts()
Winner P2 54686292 P1 45313708 Name: count, dtype: int64
Player 1 Roll 1 < 0.5
Player 1 Roll 2 Sum < 1
Player 2 Roll 1 > P1
Player 2 Roll 2 = None
df[(df['Player 1 Roll 2'] != 0) & (df['P1'] < 1) & (df['Player 2 Roll 2'] == 0)]['Winner'].value_counts()
Winner P2 14591663 Name: count, dtype: int64
Player 1 Roll 1 < 0.5
Player 1 Roll 2 Sum < 1
Player 2 Roll 1 < P1
Player 2 Roll 2 Sum < 1
df[(df['Player 1 Roll 2'] !=0) & (df['Player 2 Roll 2'] !=0) & (df['P2'] < 1)]['Winner'].value_counts()
Winner P1 8074427 P2 6766766 Name: count, dtype: int64
Player 1 Roll 1 < 0.5
Player 1 Roll 2 Sum < 1
Player 2 Roll 1 < P1
Player 2 Roll 2 Sum > 1
df[(df['Player 1 Roll 2'] !=0) & (df['P2'] > 1)]['Winner'].value_counts()
Winner P1 8072373 Name: count, dtype: int64
Player 1 Roll 1 < 0.5
Player 1 Roll 2 Sum > 1
Player 2 Roll 1 = None
Player 2 Roll 2 = None
df[df['P1'] > 1]['Winner'].value_counts()
Winner P2 12500242 Name: count, dtype: int64
Player 1 Roll 1 > 0.5
Player 1 Roll 2 = None
Player 2 Roll 1 > P1
Player 2 Roll 2 = None
df[(df['Player 1 Roll 2'] == 0) & (df['Player 2 Roll 2'] == 0)]['Winner'].value_counts()
Winner P2 12498329 Name: count, dtype: int64
Player 1 Roll 1 > 0.5
Player 1 Roll 2 = None
Player 2 Roll 1 < P1
Player 2 Roll 2 Sum < 1
df[(df['Player 1 Roll 2'] == 0) & (df['Player 2 Roll 2'] != 0) & (df['P2'] < 1)]['Winner'].value_counts()
Winner P1 14584495 P2 8329292 Name: count, dtype: int64
Player 1 Roll 1 > 0.5
Player 1 Roll 2 = None
Player 2 Roll 1 < P1
Player 2 Roll 2 Sum > 1
df[(df['Player 1 Roll 2'] == 0) & (df['Player 2 Roll 2'] != 0) & (df['P2'] > 1)]['Winner'].value_counts()
Winner P1 14582413 Name: count, dtype: int64