From 2e68fafd7dcfd4e0bfbbea32f4a4b8f63a2209ec Mon Sep 17 00:00:00 2001 From: Christina Ludwig Date: Tue, 3 Aug 2021 20:49:56 +0200 Subject: [PATCH 1/3] add randomwalk.py --- randomwalk.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 randomwalk.py diff --git a/randomwalk.py b/randomwalk.py new file mode 100644 index 0000000..ba16ba2 --- /dev/null +++ b/randomwalk.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +"""__description__ +""" + +__author__ = "Christina Ludwig, GIScience Research Group, Heidelberg University" +__email__ = "christina.ludwig@uni-heidelberg.de" + +class Walker(): + """Random Walker""" + + def __init__(self): From 97b5e08d89232a371fd0690ed635dcd76b7f9de6 Mon Sep 17 00:00:00 2001 From: Christina Ludwig Date: Tue, 3 Aug 2021 21:00:20 +0200 Subject: [PATCH 2/3] add randomwalk.py --- main.py | 46 ++++++++++++++++++---------------------------- randomwalk.py | 44 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 31 deletions(-) diff --git a/main.py b/main.py index 9aac7f0..e83addb 100644 --- a/main.py +++ b/main.py @@ -4,37 +4,27 @@ # Python code for 2D random walk. # Source: https://www.geeksforgeeks.org/random-walk-implementation-python/ -import numpy + import matplotlib.pyplot as plt -import random +from randomwalk import Walker -# defining the number of steps -n = 100000 +def main(): + # 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) + walker1 = Walker(100, 100) + walker2 = Walker(200,200) + for i in range(1, n): + walker1.walk() + walker2.walk() -# filling the coordinates with random variables -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 + # plotting the walk + plt.title("Random Walk ($n = " + str(n) + "$ steps)") + 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() -# plotting the walk -plt.title("Random Walk ($n = " + str(n) + "$ steps)") -plt.plot(x, y) -plt.savefig("./rand_walk_{}.png".format(n)) -plt.show() \ No newline at end of file +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/randomwalk.py b/randomwalk.py index ba16ba2..a8615e8 100644 --- a/randomwalk.py +++ b/randomwalk.py @@ -6,7 +6,45 @@ __author__ = "Christina Ludwig, GIScience Research Group, Heidelberg University" __email__ = "christina.ludwig@uni-heidelberg.de" -class Walker(): - """Random Walker""" +import random - def __init__(self): + +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.next_step(last_x, last_y) + self.x_coords.append(new_x) + self.y_coords.append(new_y) + + def next_step(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 From 3ffb19c7c556bef58ea80703f9c61497cb4e24e9 Mon Sep 17 00:00:00 2001 From: Christina Ludwig Date: Wed, 4 Aug 2021 08:49:47 +0200 Subject: [PATCH 3/3] small edits --- main.py | 34 +++++++++++++++++----------------- randomwalk.py | 4 ++-- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/main.py b/main.py index e83addb..337b926 100644 --- a/main.py +++ b/main.py @@ -8,23 +8,23 @@ import matplotlib.pyplot as plt from randomwalk import Walker -def main(): - # defining the number of steps - n = 100000 - - walker1 = Walker(100, 100) - walker2 = Walker(200,200) - for i in range(1, n): - walker1.walk() - walker2.walk() +import numpy +import matplotlib.pyplot as plt +import random +from randomwalk import Walker +# defining the number of steps +n = 100000 - # plotting the walk - plt.title("Random Walk ($n = " + str(n) + "$ steps)") - 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() +walker1 = Walker(100, 100) +walker2 = Walker(200, 200) +for i in range(1, n): + walker1.walk() + walker2.walk() -if __name__ == "__main__": - main() \ No newline at end of file +# plotting the walk +plt.title("Random Walk ($n = " + str(n) + "$ steps)") +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() diff --git a/randomwalk.py b/randomwalk.py index a8615e8..41d85bb 100644 --- a/randomwalk.py +++ b/randomwalk.py @@ -25,11 +25,11 @@ def walk(self): """ last_x = self.x_coords[-1] last_y = self.y_coords[-1] - new_x, new_y = self.next_step(last_x, last_y) + new_x, new_y = self.nextStep(last_x, last_y) self.x_coords.append(new_x) self.y_coords.append(new_y) - def next_step(self, last_x, last_y): + def nextStep(self, last_x, last_y): """ Choose next position randomly :return: