Skip to content

Instantly share code, notes, and snippets.

@dougmcnally
Last active September 11, 2017 19:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dougmcnally/c8f3cf1b4d0740934ed4b028c4b67276 to your computer and use it in GitHub Desktop.
Save dougmcnally/c8f3cf1b4d0740934ed4b028c4b67276 to your computer and use it in GitHub Desktop.
Python code for generating plots of 2D random walks.
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