2
$\begingroup$

I want to create a random walk using Module. The walk start at {0, 0}. At every step, x moves by a random integer in the range $[-a, a]$ and y by $[-b,b]$.

$\endgroup$

closed as off-topic by Jens, bbgodfrey, Bob Hanlon, ciao, C. E. Mar 22 '15 at 8:12

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Jens, bbgodfrey, Bob Hanlon, ciao
If this question can be reworded to fit the rules in the help center, please edit the question.

3
$\begingroup$

You can also use RandomVariate with DiscreteUniformDistribution:

rW[a_, b_, n_] := Accumulate[Prepend[
  RandomVariate[DiscreteUniformDistribution[{{-a, a}, {-b, b}}], n]], {0,0}]

dt = rW[10, 20, 100];
Graphics[{PointSize[Large], Red, Point@#, Thick, Blue, Line@#} &@dt, 
 Frame -> True, Axes->True, AspectRatio -> 1/GoldenRatio]

enter image description here

We get the same picture with:

ListPlot[{dt, dt}, Joined -> {False, True}, 
 BaseStyle -> {Thick, PointSize[Large]}, PlotStyle -> {Red, Blue}, 
 Frame -> True]
$\endgroup$
1
$\begingroup$
a = 3;
b = 5; 
randomWalk = NestList[# + {RandomInteger[{-a, a}], RandomInteger[{-b, b}]} &, {0, 0}, 100]

(* {{0, 0}, {2, 2}, {1, -3}, {-2, 2}, {1, 4}, {1, 3}, {1, 2}, {4, 0}, {4, 0}, {3, -4}, {4, -1}, {2, -5}, {0, 0}, {0, 0}, {2, -3}, {3, -6}, {2, -11}, {4, -10}, {4, -5}, {7, -3}, {9, \ -3}, {6, -8}, {9, -12}, {7, -16}, {5, -11}, {6, -16}, {4, -11}, {3, \ -15}, {4, -13}, {5, -12}, {5, -14}, {3, -16}, {6, -20}, {5, -16}, {3, \ -12}, {0, -13}, {3, -8}, {2, -7}, {1, -9}, {2, -9}, {2, -7}, {2, -4}, \ {4, -5}, {3, -7}, {4, -12}, {4, -15}, {1, -17}, {-2, -21}, {-4, -24}, \ {-3, -21}, {0, -26}, {1, -22}, {1, -20}, {-2, -15}, {0, -17}, {2, \ -12}, {3, -8}, {6, -13}, {6, -18}, {5, -23}, {5, -22}, {5, -21}, {8, \ -24}, {11, -27}, {13, -22}, {13, -17}, {15, -16}, {12, -14}, {10, \ -18}, {8, -14}, {5, -19}, {3, -21}, {0, -26}, {2, -30}, {3, -29}, {2, \ -33}, {-1, -37}, {2, -33}, {4, -38}, {4, -39}, {4, -42}, {1, -40}, \ {-1, -44}, {0, -48}, {0, -43}, {-1, -41}, {-4, -39}, {-6, -38}, {-3, \ -38}, {-1, -41}, {-1, -36}, {0, -33}, {1, -37}, {0, -42}, {-3, -39}, \ {-5, -36}, {-3, -31}, {-2, -27}, {-1, -22}, {-4, -18}, {-2, -20}} *)

Graphics[Line@randomWalk]

enter image description here

$\endgroup$
  • $\begingroup$ I'd use Accumulate, not NestList. $\endgroup$ – 2012rcampion Mar 22 '15 at 1:03
  • $\begingroup$ @2012rcampion How would you code that? $\endgroup$ – David G. Stork Mar 22 '15 at 1:15
  • $\begingroup$ Accumulate[RandomVariate[DiscreteUniformDistribution[{{-a, +a}, {-b, +b}}], n]] $\endgroup$ – 2012rcampion Mar 22 '15 at 1:20
  • $\begingroup$ @2012rcampion Doesn't include $(0,0)$ as requested, but that's easily fixed. $\endgroup$ – David G. Stork Mar 22 '15 at 1:32
  • $\begingroup$ Accumulate[_]~Prepend~{0,0}. Of course none of these solutions use Module as requested $\endgroup$ – 2012rcampion Mar 22 '15 at 1:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.