-1

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
Jacob G.
  • 28,856
  • 5
  • 62
  • 116
Ben Acton
  • 11
  • 1
  • 4
    I think -- and just a wild guess here -- you're probably dividing by zero in some place. What did your debugger say? – Davy M Feb 06 '18 at 01:38
  • 1
    Please post the _complete_ error message. – DYZ Feb 06 '18 at 01:38
  • Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. –  Feb 06 '18 at 01:40
  • [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) The answer, *"A lot. An absurd amount. More than you think you are capable of. After you have reached the end of your rope and the pain of not having the answer exceeds the vast amount of shame received by posting your question, that's when you can go ahead and ask. Because at that point, you will have done whatever research necessary to make it a good question worth asking!"* –  Feb 06 '18 at 01:40
  • The error message ends with `nratio = sumis/len(inshape)` before `ZeroDivisionError: division by zero`. Thus, `len(inshape)` is zero. Why is `inshape` empty? What should be in it? All of this should be investigated and reasoned through before calling community to help. – Amadan Feb 06 '18 at 01:42

1 Answers1

2

When you call montecarlo with the second argument of 0, it's going to pass it to randaxis, which uses it to make a dict with a bunch of empty lists as values.

Then you use the length of the first such empty list to determine the number of iterations of the first loop in montecarlo, the one that populates inshape, but of course, since it's empty, you're iterating over range(0), which doesn't iterate at all, and nothing is added to inshape.

Eventually you get here:

nratio = sumis/len(inshape)

wherein you try to divide by the length of an empty list, which is 0, and everything explodes. This will happen anytime you pass 0 as the second argument to montecarlo.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271