以下のステージ上(3次元)で乱数を生成させながら、ランダムウォークさせるときに、stageを条件文として以下のようにコードを書きましたが、うまく動くときと動かない時があります。自分としては完璧だと思ったのですが、なぜ計算されないのか頭を悩ませています。

```

    stage=[[[1. 1. 1.]
          [1. 0. 1.]
          [1. 1. 1.]]
    
         [[1. 0. 1.]
          [0. 0. 0.]
          [1. 0. 1.]]
    
         [[1. 1. 1.]
          [1. 0. 1.]
          [1. 1. 1.]]]
    
```

メイン計算

```

    import numpy as np
    import random
    import itertools
    stage = np.array([[[1,1,1],
                        [1,0,1],
                        [1,1,1]],
                       [[1,0,1],
                        [0,0,0],
                        [1,0,1]],
                       [[1,1,1],
                        [1,0,1],
                        [1,1,1]]],dtype=np.uint8)
    n = 3
    itr = 5
    step = [i for i in range(0,itr)]
    r_list = [i for i in range(0,n)]
    walk = np.zeros([n,n,n],dtype=np.uint8)
    walk[0,0,0] = 1
    #for k in range(0,1):
    for t in step:
        if t == 0:
            pass
        else:
            number = random.randint(1,6)
            next_walk = np.zeros([n,n,n],dtype=np.uint8)
            print(t,number)
            for i in itertools.product(r_list,r_list,r_list):
                x = i[0]
                y = i[1]
                z = i[2]
            #Boundary condition
                x1 = (x-1 + n) % n
                x2 = (x+1) % n
                y1 = (y-1 + n) % n
                y2 = (y+1) % n
                z1 = (z-1 + n) % n
                z2 = (z+1) % n
                if stage[i]== 0:
                    continue
    
                elif number == 1 and stage[x1,y,z]==1 and stage[x,y,z]==1:
                    next_walk[x,y,z]= np.copy(walk[x1,y,z])#*(1/6))
                elif number==1 and stage[x1,y,z]==0 and stage[x,y,z]==1:
                    next_walk[i]= walk[i]#(1/6)**t
    
                elif number == 2 and stage[x,y1,z]==1 and stage[x,y,z]==1:
                    next_walk[x,y,z]= np.copy(walk[x,y1,z])#*(1/6))
                elif number==2 and stage[x,y1,z]==0 and stage[x,y,z]==1:
                    next_walk[i]=walk[i]#(1/6)**t
    
                elif number == 3 and stage[x,y,z1]==1 and stage[x,y,z]==1:
                    next_walk[x,y,z]= np.copy(walk[x,y,z1])#*(1/6))
                elif number==3 and stage[x,y,z1]==0 and stage[x,y,z]==1:
                    next_walk[i]=walk[i]#(1/6)**t
    
                elif number == 4 and stage[x2,y,z]==1 and stage[x,y,z]==1:
                    next_walk[x,y,z]= np.copy(walk[x2,y,z])#*(1/6))
                elif number == 4 and stage[x2,y,z]==0 and stage[x,y,z]==1:
                    next_walk[i]=walk[i]#(1/6)**t
    
                elif number == 5 and stage[x,y2,z]==1 and stage[x,y,z]==1:
                    next_walk[x,y,z]= np.copy(walk[x,y2,z])#*(1/6))
                elif number==5 and stage[x,y2,z]==0 and stage[x,y,z]==1:
                    next_walk[i]=walk[i]#(1/6)**t
    
                elif number == 6 and stage[x,y,z2]==1 and stage[x,y,z]==1:
                    next_walk[x,y,z]=np.copy(walk[x,y,z2])#*(1/6))
                elif number ==6 and stage[x,y,z2]==0 and stage[x,y,z]==1:
                    next_walk[i]=walk[i]#(1/6)**t
    
                else: #stage[i]==0:
                    continue
                    #next_walk[x,y,z]=(1/6)**t
    
            walk = np.copy(next_walk)
        print(t,walk)
    
```

怪しい点、アドバイス等宜しくお願い致します。
CC BY-SA 4.0
2018-11-02T15:22:03.697
49907
2018-11-03T13:23:16.887
2018-11-03T03:41:54.257
29111
29111
question
python python3 numpy random
境界条件を含めたランダムウォーク
自己解決方法を以下に記します（万一間違っていた場合、教えて頂けると助かります）。

```

    import random
    import numpy as np
    n = 27
    itr = 10
    step = [i for i in range(0,itr+1)]
    r_list = [i for i in range(0,n)]
    walk = np.zeros([n,n,n],dtype=np.uint8)
    walk[0,0,0] = 1
    prob =[]
    time=[]
    for t in step:
        if t == 0:
            pass
        else:
            number = random.randint(1,6)
            next_walk = np.zeros([n,n,n],dtype=np.uint8)
            for i in itertools.product(r_list,r_list,r_list):
                x = i[0]
                y = i[1]
                z = i[2]
            #Boundary condition
                x1 = (x-1 + n) % n
                x2 = (x+1) % n
                y1 = (y-1 + n) % n
                y2 = (y+1) % n
                z1 = (z-1 + n) % n
                z2 = (z+1) % n
                if  stage[i]== 0:
                    continue
                else:
                    if walk[i]==1:
                        if number == 1:
                            if stage[x2,y,z]==1:
                                next_walk[x2,y,z]=walk[i]
                            else:
                                next_walk[x,y,z]=walk[i]
                        elif number == 2:
                            if stage[x,y2,z]==1:
                                next_walk[x,y2,z]=walk[i]
                            else:
                                next_walk[x,y,z]=walk[i]
                        elif number == 3:
                            if stage[x,y,z2]==1:
                                next_walk[x,y,z2]=walk[i]
                            else:
                                next_walk[x,y,z]=walk[i]
                        elif number == 4:
                            if stage[x1,y,z]==1:
                                next_walk[x1,y,z]=walk[i]
                            else:
                                next_walk[x,y,z]=walk[i]
                        elif number == 5:
                            if stage[x,y1,z]==1:
                                next_walk[x,y1,z]=walk[i]
                            else:
                                next_walk[x,y,z]=walk[i]
                        elif number == 6:
                            if stage[x,y,z1]==1:
                                next_walk[x,y,z1]=walk[i]
                            else:
                                next_walk[x,y,z]=walk[i]
                    else:
                        continue
    
```
CC BY-SA 4.0
2018-11-03T13:23:16.887
49938
2018-11-03T13:23:16.887
29111
49907
answer