Advertisement
gronke

self-avoiding random walk

Feb 19th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy
  3.  
  4. x = 0.
  5. y = 0.
  6.  
  7. visited = set()
  8. xa = []
  9. ya = []
  10.  
  11. leaver = True
  12. while leaver == True:
  13.     visited.add((x,y))
  14.     check = True
  15.     while check == True:
  16.         f = numpy.random.rand()
  17.         #temporary storage values for coords       
  18.         a = x
  19.         b = y
  20.         #pick the direction based on the seed
  21.         if f >= 0.75:
  22.             a += 1
  23.         if f >= 0.50 and f < 0.75:
  24.             a += -1
  25.         if f >= 0.25 and f < 0.50:
  26.             b += 1
  27.         if f < 0.25:
  28.             b += -1
  29.         print 'f: ',f
  30.         print 'a: ',a
  31.         print 'b: ',b
  32.         raw_input()
  33.         #check to make sure you haven't been there before
  34.         check = (a, b) in visited
  35.     x += a
  36.     y += b
  37.     xa.append(x)
  38.     ya.append(y)
  39.     print 'Moved to (',x,',',y,')'
  40.        
  41.     if numpy.abs(x) > 10: #stops at ten units out
  42.         leaver = False
  43.  
  44. plt.plot(xa,ya)
  45. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement