mardi 28 juin 2016

Numpy rand not random?

I'm coding some things on Python, and I need random numbers. I'm using the Numpy library to generate this random numbers. I have a code for a random move of an entity:

def moverse(self, Ltilda, it):
    self.historia[it] = self.pos 
    chig = abs(np.random.randn()) 
    angulo = np.random.rand() * 2.0 * np.pi 
    self.pos += np.multiply([np.cos(angulo), np.sin(angulo)], chig*Ltilda) 
    return

So I generate a gaussian variable for lenght and a uniform variable for angle. Then I call the function inside a loop. My entities can reproduce, following this:

def dividirse(self, it):
    r = np.random.rand()
    if (r < self.divide):
        return Entidad(self.pos, self.dividirse, it, len(self.historia))
    else:
        return None

If I create two different entities, the trajectories are independent. But the "childs" of entities always follow more or less the same trajectory as the parent! I have no idea why this happens. Why is the numpy.random function not giving me independent results in this case? How can I fix this? Thank you.

PS. Example for reference:

b1 = Entidad(np.array([1.0,1.0]), 0.1, 0, Nits)
aux = None
while aux == None:
    aux = b1.dividirse(0)
b1hija = aux
b2 = Entidad(np.array([1.0,1.0]), 0.1, 0, Nits)

for i in range(Nits):
    b1.moverse(0.03,i) 
    b1hija.moverse(0.03,i) #This two follows same path (more or less)
    b2.moverse(0.03,i) #This follows completely different path




Aucun commentaire:

Enregistrer un commentaire