! plots one dim brownian motion

! returns random +-1

def rndsgn=2*int(2*rnd)-1

 let a= 0
 let b= 1
 let xmin=0
 let xmax=1
 let ymin=-1
 let ymax=1

set mode "egahires"

open #1 : screen 0,1,0,1

window #1
set window xmin,xmax,ymin,ymax     
box lines  xmin,xmax,ymin,ymax
clear

sub plotbrown
  let m=2000
  let dx = (b-a)/m
  let sdx=sqr(dx)
  let x=a
  let y = 0
  plot x,y ;
  for i = 1 to m
    let x=x+dx
    let y = y + rndsgn*sdx
    plot x,y;
  next i
  plot
end sub

sub drawaxis
  set color "white"
  plot lines: xmin,0 ; xmax,0
  plot lines: 0,ymin ; 0,ymax
end sub
  
let n=0
do
 call drawaxis
 set color n
 call plotbrown
 let n=n+1
 if n>30 then
   let n=0
   clear
   call drawaxis
 end if
loop 

end
