# Programmer: Sriram Pemmaraju # Date: 3/5/2015 import random # This function returns the length of a simulated 2-dimensional # random walk with barriers at x = n, x = -n, y = n, and y = -n. # The walker starts at location (0, 0) and the walk ends on hitting the # barrier. def twoDRandomWalk(n = 100, printOn = False): # Initial location of random walker locX = 0 locY = 0 length = 0 # length of random walk while abs(locX) < n and abs(locY) < n: # Pick one of the four directions at random and # walk one step in that direction direction = random.randint(1, 4) if direction == 1: # north locY = locY + 1 elif direction == 2: # east locX = locX + 1 elif direction == 3: # south locY = locY - 1 elif direction == 4: # west locX = locX - 1 if printOn: print(locX, locY) length = length + 1 return length