Python Forum
Random walk graph not working - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Random walk graph not working (/thread-33515.html)



Random Walk, neighbors - igapo1 - May-01-2021

I've created a 3D and self-avoiding random walk, based on a dictionary containing only {'A','B','B','A','B','A',......} And made a graph out of it.

Now I need to find neighbors (A with A) that only have a distance of 1 (on the lattice) from eachother BUT are at least 3 steps (on the random walk) away from eachother.

Can somebody help? Huh


Random walk graph not working - igapo1 - May-01-2021

Hi I made a random 3D walk, (self-avoiding) and wanted to do a graph of it.
The Code is working, but for some reason I can't make the graph have lines, instead of just points...



import random as rd

random_walk(20)  
def random_walk(n):
    lys = [[0 for j in range(3)] for k in range(n)]
    x, y, z = 0, 0, 0
    i = 0
    while i < n:
        t = rd.choice([0, 1, 2])
        if t == 0:
            x += rd.choice([-1, 1])
        elif t == 1:
            y += rd.choice([-1, 1])
        elif t == 2:
            z += rd.choice([-1, 1])
        if i == 0:
            lys[i][0] = int(x)
            lys[i][1] = int(y)
            lys[i][2] = int(z)
            i += 1
        else:
            append = True
            for j in range(i):
                if lys[j][0] == x and lys[j][1] == y and lys[j][2] == z:
                    append = False
                    break
            if append == True:
                lys[i][0] = int(x)
                lys[i][1] = int(y)
                lys[i][2] = int(z)
                print(x, y, z)
                i += 1

    fig = plt.figure()
    ax = fig.gca(projection='3d')
    for i in range(n):
        ax.scatter(lys[i][0], lys[i][1], lys[i][2], c='r', marker="o")
    ax.plot(x, y, z, label='Self-avoiding random walk', color="red")
    ax.legend()
    plt.show()

    return x, y, z