In [1]:
# mtg situation:
# opponent casts boring sorcery, I cast flusterstorm with storm = X
# I control a grip of chaos
# no mana for anyone
In [2]:
import random
In [3]:
def resolves(storm):
# create stack, 0 is original spell, 1 is the first fluster (that will always target the original spell)
stack = range(storm+2)
# print " stack: ", stack
# redirect each copie of fluster (except the first one)
redirections = [0, 0] #first two spell on the stack do not get redirected
for copy in stack[2:]:
target = copy
# choose another target (not itself)
while target == copy:
target = random.randint(0,storm+1)
redirections.append(target)
# print "redirections: ", redirections
# resolve copies in backwards order
pos = storm +2
for target in reversed(redirections[2:]):
pos -= 1
if target > pos:
# target is already resolved
pass
elif stack[target] == '-1' :
# target has been previously countered
pass
else:
stack[target] = -1
# print " stack: ", stack
return stack[0] == 0
In [4]:
print resolves(10)
In [5]:
# perform experiments
distribution = [0]
for storm in range(100)[1:]:
distribution.append(0)
# do each exp k times
k = 5000
for x in range(k):
if resolves(storm):
distribution[storm] += 1
# normalize
distribution[storm] /= float(k)
In [6]:
print distribution
In [7]:
# plot
import pylab
import matplotlib.pyplot as plt
%matplotlib inline
pylab.rcParams['figure.figsize'] = (20.0, 8.0) #adjust to your screen
In [8]:
plt.plot(distribution)
Out[8]:
In [ ]: