Re: Faster Random Walk Simulation ?!?
- To: mathgroup at smc.vnet.net
- Subject: [mg66211] Re: Faster Random Walk Simulation ?!?
- From: "Chris Chiasson" <chris.chiasson at gmail.com>
- Date: Thu, 4 May 2006 05:20:47 -0400 (EDT)
- References: <e39k1n$cmn$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Some ideas (do you really need that index of positions?) In[1]:= SeedRandom[5000] In[2]:= RandomWalk[value_?(GreaterEqual[1,#, 0]&),boundaryA_?Positive]:= MapIndexed[{#2[[1]]-1,#1}&,NestWhileList[#+If[Random[]>value,1,-1]\ &,0,Abs[#]<boundaryA&]] In[3]:= Timing[Table[RandomWalk[1/2,5],{i,1,10000}];] Out[3]= {2.36 Second,Null} In[4]:= Last/@RandomWalk[1/2,5] Out[4]= {0,-1,-2,-1,-2,-1,0,1,0,1,0,1,0,-1,0,-1,-2,-1,-2,-3,-4,-5} In[5]:= Clear[RandomWalk] In[6]:= SeedRandom[5000] In[7]:= RandomWalk[value_?(GreaterEqual[1,#, 0]&),boundaryA_?Positive]:=NestWhileList[#+If[ Random[]>value,1,-1]&,0,Abs[#]<boundaryA&] In[8]:= Timing[Table[RandomWalk[1/2,5],{i,1,10000}];] Out[8]= {1.438 Second,Null} In[9]:= RandomWalk[1/2,5] Out[9]= {0,-1,-2,-1,-2,-1,0,1,0,1,0,1,0,-1,0,-1,-2,-1,-2,-3,-4,-5} In[10]:= Clear[RandomWalk] In[11]:= SeedRandom[5000] In[12]:= RandomWalk[value_,boundaryA_]:=NestWhileList[#+If[Random[]>value,1,-1]&,0,Abs[\ #]<boundaryA&] In[13]:= Timing[Table[RandomWalk[1/2,5],{i,1,10000}];] Out[13]= {1.437 Second,Null} In[14]:= RandomWalk[1/2,5] Out[14]= {0,-1,-2,-1,-2,-1,0,1,0,1,0,1,0,-1,0,-1,-2,-1,-2,-3,-4,-5} In[15]:= Clear[RandomWalk] In[16]:= SeedRandom[5000] In[17]:= value=1/2 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[17]= \!\(1\/2\) Out[19]= {2.281 Second,Null} In[20]:= Last/@RandomWalk[5] Out[20]= {0,-1,-2,-1,-2,-1,0,1,0,1,0,1,0,-1,0,-1,-2,-1,-2,-3,-4,-5}