I get a division by zero error when I run this code, can anyone see why? The montecarlo and Vd functions both work perfectly fine until I put them in the for loop at the end and I can't see why, there shouldn't be a zero anywhere to cause the error in the first place.
import numpy as np
import matplotlib.pyplot as plt
import random
d=2
def randaxis(d,N):
axes={}
for i in range(0,d):
axes[i]=[]
for j in range(0,N):
axes[i].append(random.uniform(-1,1))
return axes
def montecarlo(d,N):
coords=randaxis(d,N)
inshape=[]
for i in range(len(list(coords.values())[0])):
sumsqs=0
for v in coords.values():
sumsqs+=v[i]**2
if sumsqs<=1:
inshape.append(1)
else:
inshape.append(0)
sumis=0
for i in inshape:
sumis+=i
nratio = sumis/len(inshape)
return nratio
def Vd(nratio):
vd = nratio*(2**d)
return vd
Rep=10
k=0
Repeats=np.zeros(Rep)
N=50
for N in range(1000):
for k in range(Rep):
Repeats[k]=Vd(montecarlo(2,N))
print(Repeats)
N+=50