MathGroup Archive 2006

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

Search the Archive

Re: Faster Random Walk Simulation ?!?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg66232] Re: Faster Random Walk Simulation ?!?
  • From: Roland Franzius <roland.franzius at uos.de>
  • Date: Fri, 5 May 2006 05:02:05 -0400 (EDT)
  • Organization: Universitaet Hannover
  • References: <e39k1n$cmn$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

mfific at gmail.com schrieb:
> 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}

Does not save much time but a  bit

Step=(2*Random[Integer, {0, 1}] + # - 1) &

cd[bound_] :=
   (Transpose[{Range[1, Length[#]], #}] &)[
NestWhileList[Step,0, Abs[ #] < bound &]  ]


Timing[Table[cd[5], {10000}];]
{1.472 Second, Null}

Mathematica 5.2  Windows XP  Centrino 1.5 MHz running at 500 MHz

{1.332 Second, Null}  running full speed at 1.5 MHz

-- 

Roland Franzius


  • Prev by Date: Re: How to find expected value?
  • Next by Date: Re: How to find expected value?
  • Previous by thread: Re: Faster Random Walk Simulation ?!?
  • Next by thread: Re: Faster Random Walk Simulation ?!?