-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrandomwalk.py
More file actions
50 lines (43 loc) · 1.18 KB
/
randomwalk.py
File metadata and controls
50 lines (43 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""__description__
"""
__author__ = "Christina Ludwig, GIScience Research Group, Heidelberg University"
__email__ = "christina.ludwig@uni-heidelberg.de"
import random
class Walker:
def __init__(self, init_x=500, init_y=500):
"""
Create walker object
"""
self.x_coords = [init_x]
self.y_coords = [init_y]
def walk(self):
"""
Walker walks one step forward in random direction
:return:
"""
last_x = self.x_coords[-1]
last_y = self.y_coords[-1]
new_x, new_y = self.nextStep(last_x, last_y)
self.x_coords.append(new_x)
self.y_coords.append(new_y)
def nextStep(self, last_x, last_y):
"""
Choose next position randomly
:return:
"""
val = random.randint(1, 4)
if val == 1:
new_x = last_x + 1
new_y = last_y
elif val == 2:
new_x = last_x - 1
new_y = last_y
elif val == 3:
new_x = last_x
new_y = last_y + 1
else:
new_x = last_x
new_y = last_y - 1
return new_x, new_y