2
$\begingroup$

Suppose that we have a 4 faces die. The person toss the die and if it land on:
1) the person will move to the right
2) the persone will move to the left
3) the person will move upward
4) the person will move downward

The process will repeat again by tossing the coin and move from the last position and so on for n more times.

How can I generate this random simulation.

test[n_] := Accumulate[RandomChoice[{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}, n]]

How can i show the last point that it is plot if I use this function ListLinePlot[test[10]]?

$\endgroup$
  • $\begingroup$ Look at RandomChoice $\endgroup$ – Andy Ross Oct 5 '15 at 2:10
  • $\begingroup$ See mathematica.stackexchange.com/q/57561/18476 $\endgroup$ – Karsten 7. Oct 5 '15 at 2:20
  • $\begingroup$ Also mathematica.stackexchange.com/a/78351 $\endgroup$ – Karsten 7. Oct 5 '15 at 2:25
  • $\begingroup$ test[n_] := Accumulate[RandomChoice[{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}, n]], How can i plot this, $\endgroup$ – seito Oct 5 '15 at 2:37
  • $\begingroup$ ListLinePlot[#, PlotRange -> All, Mesh -> All, Epilog -> {Red, Point@Last@#}, AspectRatio -> Automatic] &@test[10] $\endgroup$ – Karsten 7. Oct 5 '15 at 3:14
3
$\begingroup$

You might implement your random walk something like this.

walk[n_] := 
  Prepend[
    Accumulate[RandomChoice[{{-1, 0}, {1, 0}, {0, 1}, {0, -1}}, n]], 
    {0, 0}]

trial[n_] := 
  Module[{path, start, end},
    path = walk[n];
    start = First @ path;
    end = Last @ path;
    ListLinePlot[path,
      Epilog -> {PointSize[Large], Point[start], Red, Point[end]},
      AspectRatio -> Automatic]]

SeedRandom[3]; trial[25]

walk

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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