Why did a random-walk simulation that printed plausible numbers turn out wrong, and how did a 0-, 1-, 2-step check expose it?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
Location, Drunk, UsualDrunk and Field are given. Define walk(f, d, numSteps): move drunk d in field f numSteps times and return the distance between where d ends and where it started.
import random
class Location(object):
def __init__(self, x, y):
self.x = x
self.y = y
def move(self, deltaX, deltaY):
return Location(self.x + deltaX, self.y + deltaY)
def getX(self):
return self.x
def getY(self):
return self.y
def distFrom(self, other):
xDist = self.x - other.getX()
yDist = self.y - other.getY()
return (xDist**2 + yDist**2)**0.5
def __str__(self):
return '<' + str(self.x) + ', ' + str(self.y) + '>'
class Drunk(object):
def __init__(self, name=None):
self.name = name
def __str__(self):
if self.name != None:
return self.name
return 'Anonymous'
class UsualDrunk(Drunk):
def takeStep(self):
stepChoices = [(0, 1), (0, -1), (1, 0), (-1, 0)]
return random.choice(stepChoices)
class Field(object):
def __init__(self):
self.drunks = {}
def addDrunk(self, drunk, loc):
if drunk in self.drunks:
raise ValueError('Duplicate drunk')
else:
self.drunks[drunk] = loc
def getLoc(self, drunk):
if drunk not in self.drunks:
raise ValueError('Drunk not in field')
return self.drunks[drunk]
def moveDrunk(self, drunk):
if drunk not in self.drunks:
raise ValueError('Drunk not in field')
xDist, yDist = drunk.takeStep()
self.drunks[drunk] = self.drunks[drunk].move(xDist, yDist)