任务是
Write a function
collect_sims(nsim,N,D,p=0.5,nmax=10000)
that runs yourrun_sim
functionnsim
times (with parametersN
,D
,p
) and returns a numpy array of lengthnmax
giving the number of times that the simulation took a specified number of steps to stop.For example, suppose
nsim
was 8 and successive runs ofrun_sim
gave you 3,4,4,3,6,5,4,4.
You would tabulate this as “two 3s, four 4s, one 5, one 6, zero 7s, zero 8s …”
此函数调用的函数是
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))
我写道:
^{pr2}$它没有给我一个错误,但它也没有返回或打印任何东西,当我运行它,我做错了什么?在
目前没有回答
相关问题 更多 >
编程相关推荐