Last active
September 11, 2017 19:20
-
-
Save dougmcnally/c8f3cf1b4d0740934ed4b028c4b67276 to your computer and use it in GitHub Desktop.
Python code for generating plots of 2D random walks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
import pylab | |
import random | |
n = 100000 | |
x = numpy.zeros(n) | |
y = numpy.zeros(n) | |
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 stuff: | |
pylab.title("Random Walk ($n = " + str(n) + "$ steps)") | |
pylab.plot(x, y) | |
pylab.savefig("rand_walk"+str(n)+".png",bbox_inches="tight",dpi=600) | |
pylab.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment