Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,27 @@

# Python code for 2D random walk.
# Source: https://www.geeksforgeeks.org/random-walk-implementation-python/

import matplotlib.pyplot as plt
from randomwalk import Walker

import numpy
import matplotlib.pyplot as plt
import random
from randomwalk import Walker
Comment on lines 12 to +14
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the imports. Some are duplicated.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thanks! Will fix it.


# defining the number of steps
n = 100000

# creating two array for containing x and y coordinate
# of size equals to the number of size and filled up with 0's
x = numpy.zeros(n)
y = numpy.zeros(n)

# filling the coordinates with random variables
walker1 = Walker(100, 100)
walker2 = Walker(200, 200)
for i in range(1, n):
val = random.randint(1, 4)
if val == 1:
x[i] = x[i - 1] + 1
y[i] = y[i - 1]
elif val == 2:
x[i] = x[i - 1] - 1
y[i] = y[i - 1]
elif val == 3:
x[i] = x[i - 1]
y[i] = y[i - 1] + 1
else:
x[i] = x[i - 1]
y[i] = y[i - 1] - 1

walker1.walk()
walker2.walk()

# plotting the walk
plt.title("Random Walk ($n = " + str(n) + "$ steps)")
plt.plot(x, y)
plt.plot(walker1.x_coords, walker1.y_coords)
plt.plot(walker2.x_coords, walker2.y_coords)
plt.savefig("./rand_walk_{}.png".format(n))
plt.show()
plt.show()
50 changes: 50 additions & 0 deletions randomwalk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool class! :)


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)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please stick to PEP8 and make the method snake_case (self.next_step)

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