Advertisement
AmCholadawan

levywalk

Mar 6th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. import random
  2. from datetime import datetime
  3. from scipy.stats import levy
  4.  
  5. random.seed(datetime.now())
  6.  
  7. def direction():
  8.  
  9.     """pick an initial random direction with uniform distribution"""
  10.     direction_xy = polarToCartesian(random.randint(0, 7))
  11.  
  12.     # custom parameters
  13.     stride_normal = 1
  14.     stride_long = 30
  15.     loc = 0
  16.     scale = 1
  17.     threshold = 25
  18.  
  19.     """stride length using a random pick from levy distribution"""
  20.     r = levy.rvs(loc, scale)
  21.  
  22.     # use < to enable stride on tail
  23.     # use > to enable stride  on head
  24.     stride = stride_normal if r < threshold else stride_long
  25.  
  26.     #print([direction_xy, stride])
  27.     return([direction_xy, stride])
  28.  
  29. def polarToCartesian(direction):
  30.     # 0 1 2
  31.     # 7 b 3
  32.     # 6 5 4
  33.     return {
  34.         0: [-1, -1],
  35.         1: [-1,  0],
  36.         2: [-1, +1],
  37.         3: [ 0, +1],
  38.         4: [+1, +1],
  39.         5: [+1,  0],
  40.         6: [+1, -1],
  41.         7: [ 0, -1]
  42.     } [direction]
  43.  
  44. ### TEST
  45. for t in range(10):
  46.     print(direction())
  47. ###
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement