Random Walks in python

Viewed 35

There are classes for Location, Drunk, and Field. I was trying to have a subclass dirty field. That generates a dirty tile in the field. If a random walker moves on the dirty tile then, they keep moving.

I am getting the following error: I am hoping that y'all see something that I don't see. edit: I have fixed the grammar errors and now when running I am getting a randint() error. When looking into randint should I change it to uniform?

KeyboardInterrupt                         Traceback (most recent call last)
Input In [20], in <cell line: 27>()
     22         self.party[motive] =\
     23                 self.party[motive].move(x, y)
     26 start = Location(0, 0)
---> 27 f = dirtyField()
     29 homer = SouthDrunk('Homer')
     30 f.addDrunk(homer, start)

Input In [20], in dirtyField.__init__(self, dirtyTiles, xRange, yRange)
      6 w = 0
      7 while (w < dirtyTiles):
----> 8     x = random.randint(-xRange, xRange)
      9     y = random.randint(-yRange, yRange)
     10     aDirtyTile = Location(x, y)

File ~\anaconda3\lib\random.py:338, in Random.randint(self, a, b)
    334 def randint(self, a, b):
    335     """Return random integer in range [a, b], including both end points.
    336     """
--> 338     return self.randrange(a, b+1)

File ~\anaconda3\lib\random.py:314, in Random.randrange(self, start, stop, step)
    312 width = istop - istart
    313 if step == 1 and width > 0:
--> 314     return istart + self._randbelow(width)
    315 if step == 1:
    316     raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))

File ~\anaconda3\lib\random.py:243, in Random._randbelow_with_getrandbits(self, n)
    241     return 0
    242 getrandbits = self.getrandbits
--> 243 k = n.bit_length()  # don't use (n-1) here because n can be 1
    244 r = getrandbits(k)  # 0 <= r < 2**k
    245 while r >= n:

KeyboardInterrupt:
class dirtyField(Field):
    def __init__(self, dirtyTiles = 1000,
                 xRange = 100, yRange = 100):
        Field.__init__(self)
        self.dirtTile = []
        w = 0
        while (w < dirtyTiles):
            x = random.randint(-xRange, xRange)
            y = random.randint(-yRange, yRange)
            aDirtyTile = Location(x, y)
            self.dirtTile.append(aDirtyTile)
            
    def moveDrunk(self, motive):
        # per instructions if the axis is a dirty tile then the drunk moves until a clean tile.
        # one tile at a time motive is another
        Field.moveDrunk(self, motive)
        while (self.party[motive] in self.dirtTiles):
            self.party[motive] =\
                self.party[motive].move(x, y)
            x, y = motive.takeStep()
            
        self.party[motive] =\
                self.party[motive].move(x, y)

start = Location(0, 0)
f = dirtyField()

homer = SouthDrunk('Homer')
f.addDrunk(homer, start)
f.moveDrunk(homer)
print(f.getLoc(homer))
1 Answers

I had grammar errors and def moveDrunk was too much. I also needed w+=1 at the init

Related