samedi 23 mai 2015

Python generating large amounts of random numbers (5000-8000) quickly

I need to generate two random numbers, a and b, in the range of 1-5, about 5000 times. Then I sum all the a's and all the b's and compare them, returning the difference of the sums.

When I try to do this using the default random.randint or random.randrange I get an unacceptable level of time delay (~15 seconds). Is there a faster way to do this?

Here is the relevant code:

    total_damage = 0 
    attack_score = 0
    defence_score = 0
    enemy_list = []

    for team in teams:
        for unit in team:
            if unit.team != self.team:
                enemy_list.append(unit)

    for enemy in enemy_list:
        if (enemy.x, enemy.y) == (enemy_x, enemy_y):
            for i in range(int(self.power*self.unit_size)):
                attack_score += random.randint(0, self.power)
            for i in range(int(enemy.defence*enemy.unit_size)):
                defence_score += random.randint(0, enemy.defence)
            if attack_score > defence_score:
                total_damage = attack_score - defence_score
            else:
                total_damage = 0




Aucun commentaire:

Enregistrer un commentaire