I'm running the random walk function in R using iGraph, where my random walker is traveling along a road network. However, when I run the below code I find that the random walker always goes back to the same vertices/nodes. Is there a way I can program the random walker to follow a new path each time?
set.seed(1)
resample <- function(x, ...) x[sample.int(length(x), ...)]
n <- 1000
tm <- G
x = sub$idNode1[sample(1:length(sub$idNode1),1)]
start <- x # Random walk starting vertex
len <- 100 # Walk length
path <- c(start, rep(NA, len))
for(i in 2:(len + 1)) {
idx <- G[path[i - 1], ] != 0
if(any(idx)) {
path[i] <- resample(which(idx), 1, prob = G[path[i - 1], idx])
} else {
break # Stopping if we get stuck
}