作业是
编写一个运行
collect_sims(nsim,N,D,p=0.5,nmax=10000)
函数run_sim
次的函数nsim
(参数N
,D
,p
)并返回长度为nmax
的numpy数组,给出模拟停止指定步骤数的次数。例如,假设
nsim
为8,run_sim
的连续运行为您提供3,4,4,3,6,5,4,4。 您可以将其列为“两个3,四个4,一个5,一个6,零7,零8 ...”
此函数调用的函数是
def run_sim(N=20, D=6, p=0.5, itmax=5000):
counter = N
sum = 0
i = 0
while counter != 0:
if i>itmax:
raise RuntimeError
c = np.random.randint(1,D+1,1)[0] # method returns an array with 1 value
coin = np.random.binomial(1,p,1)[0] # if =1 is heads, =0 is tails
print(i,"|",c,"|",counter)
if coin ==1:
if (counter + c)>N:
i+=1
else:
i=+0
counter = counter +c
elif coin ==0:
print("coint == 0: counter - c:", counter - c)
if(counter - c)<0:
i+=1
else:
i+=1
counter = counter -c
print("coint ==0: counter:", counter)
if counter==0:
break
return(i)
print(run_sim(N=20, D=6, p=0.5,itmax=500))
print(run_sim(N=20, D=12, p=0.5, itmax=500))
我写道:
def collect_sims(nsim, N, D, p=0.5, nmax=10000):
run_sim(N=N, D=D, p=0.5, itmax=5000)
a=np.zeros(nmax)
a.reshape(nsim,2)
for i in range (nsim):
a[run_sim(N,D,p),2] += 1
return(a)
它没有给我一个错误,但是当我运行它时它也没有返回或打印任何东西,我做错了什么?