Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python数组计数器_Python_Arrays_Python 3.x_Numpy - Fatal编程技术网

Python数组计数器

Python数组计数器,python,arrays,python-3.x,numpy,Python,Arrays,Python 3.x,Numpy,任务是 编写一个函数collect\u sims(nsim,N,D,p=0.5,nmax=10000),运行run\u sim函数nsim次(带参数N,D,p)并返回一个长度为nmax的numpy数组,给出模拟停止指定步数的次数 例如,假设nsim为8,连续运行run\u 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): cou

任务是

编写一个函数
collect\u sims(nsim,N,D,p=0.5,nmax=10000)
,运行
run\u sim
函数
nsim
次(带参数
N
D
p
)并返回一个长度为
nmax
的numpy数组,给出模拟停止指定步数的次数

例如,假设
nsim
为8,连续运行
run\u 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))

它没有给我一个错误,但当我运行它时,它也没有返回或打印任何内容,我做错了什么?

显示实际调用此函数的代码。再次查看
运行_sum
也很有用,请阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。
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)