# Random walk in 1 D # # From Seefeld and Linder, "Statistics Using R with Biological Examples." # Modified by MVW 2018-01-31 # # Inputs: (default) # n Number of steps to compute (100) # p Prob(x,x+1) (0.5) # seed Random number seed (NULL) # # Output: # Plot # Walk1d<-function(n=100, p=0.5, seed=NULL) { if( !is.null(seed) ) set.seed(seed); y<-vector(length=n); y[1]<-0; for(i in 2:n) y[i]<-y[(i-1)]+sample(c(-1,1),1,prob=c(1-p,p)); lim <- max(c(y,abs(y)))+3; plot(1:n,y,type='l',ylim=c(-lim,lim)); }