mardi 5 mai 2020

generating a random number everytime in a loop in python

I am trying to simulate blobs reproducing and dying and every blob has a reproduction- and death chance. Every time in a loop it is supposed to generate a random rumber to see if the death and birth rate is bigger than the random number, if yes, create or delete a blob. every blob is stored in a list blobs.

THe problem is that it seems to generate 1 number for all blobs so eiter no one dies or everyone dies at once, but i want different randoms for each blob. For example reproduction -and deathchance are 0.5.

for blob in blobs:
        if blob.reproduce_chance > random.uniform(0,1):
            blobs.append(blob)
        if blob.death_chance > random.uniform(0,1):
            blobs.pop(blobs.index(blob))



Aucun commentaire:

Enregistrer un commentaire