#random walk program, Ted Einstein, 11/03
from visual import *
from visual.graph import *
from random import random

walkerx = 0
xlist=range(100)
xlist[0]=0

xgraph = gdisplay(height=200, width=800, 
            title='Plot of Displacement vs. Time',
            xtitle='Time (steps)', ytitle='x',
            background=(1,1,1), foreground=(0,0,1)) 
xsqgraph = gdisplay(y=200,height=200, width=800, 
            title='Plot of "Diffusion Const." vs. Time',
            xtitle='Time (steps)', ytitle='x**2/time',
            background=(1,1,1), foreground=(0,0,1))
xhist = gdisplay(y=400, width=800, xmax=200,
            title='Histogram of Abs(Displacement)',
            xtitle='Displacement',
            background=(1,1,1), foreground=(0,0,1))
        
        
xdist = ghistogram(gdisplay=xhist,bins=arange(0, 200, 10), color=color.red)
                      
funct1 = gcurve(gdisplay=xgraph,color=color.blue)
funct3 = gcurve(gdisplay=xsqgraph,color=color.red)

tmax=10000

for i in range(1,tmax):
    rate(200)
    p=random()
    if p < 0.5:
        walkerx=walkerx + 1
    else:
        walkerx=walkerx - 1
    if walkerx ==0: 
        print "Walker crosses origin at timestep %d"   %i
    funct1.plot(pos=(i, walkerx))
    xsq=walkerx**2
    funct3.plot(pos=(i, float(xsq)/i))
    xlist[i%100]=abs(walkerx)
    if i%100==0:
 #       print xlist
        xdist.plot(data=xlist,accumulate=1)


    
  
  	

