Python Forum
Random walk graph not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random walk graph not working
#1
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
Reply
#2
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
buran write May-01-2021, 08:35 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Could someone please help walk me through my home work tetoronga 3 3,015 Mar-22-2018, 09:33 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020