# random walk
runs <- 5
eff_seeds <- sample(1:2^15, runs)
N <- 1E5
for(q in 1:runs){
set.seed(eff_seeds[q])
x <- sample(c(-1,0,1),size = N,replace = TRUE)
y <- sample(c(-1,0,1),size = N,replace = TRUE)
diff <- abs(x) - abs(y)
throw <- which(diff %in% 0)
x <- x[-throw]
y <- y[-throw]
# sumx <- vector(length = length(x))
# sumy <- vector(length = length(y))
sumx <- integer(length = length(x))
sumy <- integer(length = length(y))
# system.time(for(i in 1:length(x)) {sumx[i] <- sum(x[1:i]); sumy[i] <- sum(y[1:i])} )
for(i in 2:length(x)) {sumx[i] <- sumx[i-1]+x[i]; sumy[i] <- sumy[i-1]+y[i]} #This is much faster
# sumx <- c(0,sumx)
# sumy <- c(0,sumy)
plot(0,
xlim = c(min(sumx)*1., max(sumx)*1.),
ylim = c(min(sumy)*1.,max(sumy)*1.),
xaxt = "n", yaxt = "n", bty = "n",
xlab = "", ylab = "",
type = "n", asp = 1)
lines(sumx,sumy, lwd = .5)
points(0,0, pch = 16, col = "red")
points(sumx[i],sumy[i], pch = 16, col = "blue")
# abline(h=0, col = "lightgrey"); abline(v=0, col = "lightgrey")
mtext(paste("Random Walk: ", i, "steps Seed: ", eff_seeds[q]), side = 1, adj = 0, cex = .75, line = 2)
}