Message Boards Message Boards

0
|
1267 Views
|
1 Reply
|
2 Total Likes
View groups...
Share
Share this post:

Create a 2 dimensional random walk with a do loop?

Posted 5 years ago

I am trying to create a 2 dimensional random walk that has an equal chance to do up, down, right, or left with a do loop and which statement, but I am not sure I have the correct coding.

M = Table[0, {1000}, {2}];

Do[x = RandomReal[];
  Which[0 <= x <= 0.25, y = 1,
   0.25 <= x <= 0.50, y = -1,
   0.50 <= x <= 0.75, y = 1,
   0.75 <= x <= 1.0, y = -1];
  M[[i + 1, j]] = y + M[[i, j]],
  {i, -999, 999}, {j, -1, 2}];
Grid[M]
POSTED BY: Jennifer Shaw

I don't know what you have in mind with "create", but this is a simple visualization:

stp := RandomChoice[{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}];
pt = {0, 0}; Dynamic[Graphics[Point[pt = pt + stp], PlotRange -> 50]]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract