Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Faster Random Walk Simulation ?!?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg66215] Re: Faster Random Walk Simulation ?!?
  • From: "Ray Koopman" <koopman at sfu.ca>
  • Date: Thu, 4 May 2006 05:20:54 -0400 (EDT)
  • References: <e39k1n$cmn$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Here is a minor rewrite of your RandomWalk, run on a slower machine:

RandomWalk[v_,bA_] := Block[{L = {{0, 0}}, x = 0, i = 0 },
While[bA > x > -bA,
      x += If[Random[] > v, 1, -1];
      L = Append[L, {++i, x}]];
    L]

Timing[Table[RandomWalk[.5,5], {i, 1, 10^4}];]

{3.74 Second,Null}

Here is a faster version:

RandomWalk2[v_,bA_] := Transpose@{Range[0,Length@#-1],#}& @
NestWhileList[# + If[Random[] > v, 1, -1] &, 0, bA > # > -bA &]

Timing[Table[RandomWalk2[.5,5], {10^4}];]

{2.51 Second,Null}

Do you really need the step numbers?

RandomWalk2a[v_,bA_] := NestWhileList[
# + If[Random[] > v, 1, -1] &, 0, bA > # > -bA &]

Timing[Table[RandomWalk2a[.5,5], {10^4}];]

{2.35 Second,Null}

You should be aware that there are problems with Random[]. I do not
use it for anything in which the lack of independence of successive
values might conceivably matter. See my June 16, 2003, post "Warning
-- another Random[] failure". For much more on this, see the thread
"normal distribution random number generation" that started Oct 4,
2004.

mfific at gmail.com wrote:
> Dear All,
> I am running a simulation with Mathematica 5 that includes random walk
> described below. The argument boundaryA is upper and lower boundary for
> a random walk. Parameter "value" is just a constant arbitrarily set to
> 0.5. The output of the table function is the 10000 random walks, and
> their path to either +5 or -5 boundary value.
> While everything works as expected, it takes considerable duration of
> time. For example it takes 2.6 seconds to generate the table output, on
> a relatively fast computer.
>
> I would very appreciate if any significantly faster solution could be
> suggested.
>
>
> value = .5
>
>
> RandomWalk[boundaryA_] := Block[{l = {{0, 0}}, x = 0,
>   i = 0 }, While[boundaryA > x > -boundaryA,
>        x += If[Random[] > value, 1, -1];
>       l = Append[l, {++i, x}]];
>     l]
>
> Timing[Table[RandomWalk[5], {i, 1, 10000}];]
>
> Out[420]=
> {2.672 Second, Null}
>
>
> Thank you very much,
>
> Mario Fific
>
>
> Mario Fific
> Cognitive Psychology, Cognitive Science
> Indiana University
> 1101 E. 10th St.
> Bloomington, IN  47405-7007


  • Prev by Date: Re: Faster Random Walk Simulation ?!?
  • Next by Date: Re: Selecting Many Things Rather Than Selecting One Thing From Many
  • Previous by thread: Re: Faster Random Walk Simulation ?!?
  • Next by thread: Re: Faster Random Walk Simulation ?!?