NPC Movement problem

So I am trying to have an AI-controlled NPC walk around my level. The algorithm that I am using works, except the NPC doesn’t face the right way. I use the same algorithm to have the NPC run at the main character, and he faces the right direction. Yet, when I use it to make the NPC just walk around, whenever he changes direction, he faces a totally random direction instead of facing the direction that he is going. Can someone tell me what is going wrong with my code, or show me a better way to do this.
The code I am using right now is:

    if ((self.counter%175) == 0):
        self.theta1 = (random.random()*359)

    self.dX1 = (math.cos(self.theta1)*.17) # this is how far the NPC moves on the X-axis

    self.dY1 = (math.sin(self.theta1)*.17) # this is how far the NPC moves on the Y-axis

    if (abs(self.deltaX) >= 80 or abs(self.deltaY) >= 80):
        
                    self.badGuy.setH(self.theta1)

        self.badGuy.setPos(self.badGuy.getX()+self.dX1,  self.badGuy.getY()+self.dY1, 0)

You may want to use the code tag or otherwise the indentation of your posted code is lost.

Also, how is counter and deltaX, deltaY being updated ? More code may help.

TheUsualSuspect, download and take a look to the code in here - there is lot of interesting stuff to dig into about this topic