0

In this link, the author implemented a simulation of a 2D random walk.

private void RunScript(int seed, int time, ref object A)
{
    List <Point3d> pList = new List<Point3d>();

    Walker w = new Walker();
    Random random = new Random(seed);

    for(int i = 0; i < time; i++){
        int rnd = random.Next(0, 4);
        w.step(rnd);
        pList.Add(w.pos());
    }

    A = pList;
}

public class Walker
{
    public int x;
    public int y;
    public int rnd;

    public Walker(){
        x = 0;
        y = 0;
        rnd = 0;
    }

    public Point3d pos(){
        Point3d posPt = new Point3d(x, y, 0);
        return posPt;
    }

    public int step(int rnd){
        int choice = rnd;
        if (choice == 0){
            x++;
        }
        else if( choice == 1){
            x--;
        }
        else if(choice == 2){
            y++;
        }
        else if(choice == 3){
            y--;
        }

        return choice;
    }
}

According to this code, either x or y could be changed(incremented or decremented) at one step.

Is this consistent with the theory of Random Walk?

Shouldn't he have generated two random choices(one for x and another for y) and change both x and y at each step?

user366312
  • 1,641
  • You can see another example here. Notice the sampling of increments from: ${ {1,0}, {0,1}, {-1,0}, {0,-1}$ which is equivalent to the above. – algae May 20 '20 at 05:51
  • You are doing what is called "reverse engineering" (no tag in Math SE for it, a pity). Do this in very scarce cases : you can loose much time on it. A faulty programmer that do not document properly his/her code is hopefully also a poor coder with possible erroneous methods. – Jean Marie May 20 '20 at 06:22
  • OP: Your choice would move on diagonals; this code moves on orthogonal lines. Both are random walks. – Brian Tung May 20 '20 at 07:34
  • Instead of reverse engineering, can we give the prototypical case for random walk on 2D lattice? Do we select probabilities for jumps on each coordinate independently or not, when and why etc. – algae May 20 '20 at 08:45

0 Answers0