Refine the random walk algorithm