I have code which generates many N sequences and stores them. If I make runs large then the loop can get stuck on the while N[j] > 0: section of the code. What is the best way to include something to skip a step in the while loop, say if time exceeds a few seconds in that step in the loop and move to the next?
runs = 20
Lloc = 1
n0 = 1
#Empty list to store N series
N_array = []
#Empty list to store z series
z_array = []
for r in range(0,runs):
#Set initial z series values to be zero
z = [0]
#Set initial jump process values to be n0
N = [n0]
#Set iteration to be zero
j = 0
#While
while N[j] > 0:
z.append(z[j] + np.random.exponential(Lloc/(2*N[j]**2)))
#Pick jump at position j+1 to be N[j] -1 or +1 with prob 1/2
N.append(N[j] + np.random.choice([-1,1]))
#Update iteration
j = j+1
#Store N,z realisation if sum dz < 10 say
if sum(np.diff(z)) < 10:
N_array.append(N)
z_array.append(z)
#Completion
print((r+1)/runs*100,'%')