vendredi 29 décembre 2017

Difficulty initiating Random with Float to Integer

For my program, I am trying to get a random range with variables I can input, but it seems to cause the program to crash when I try to work it. I have pinpointed where the main problem is, though:

float flux = stability/100;
int max = (int)(attack * (1 + flux));
int min = (int)(attack * (1 - flux));
for (int i = 0; i < attacksPerMinute; ++i) {
    damage = 0;
        for (int j = 0; j < ammo; ++j) {
            damage += damageFlux(min, max);
        }
    minuteDamage += damage;
}

damageFlux is thus:

private static int damageFlux(int min, int max) {

if (min >= max) {
    throw new IllegalArgumentException("max must be greater than min");
}

Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}

Now, in my head, the min and max should work, but its not. It causes the error to be thrown, most likely because its not doing the math to cause the pair to be different. Here are a couple variables on how it should work:

If attack is 10 and the flux is .20, then max should be (10 * (1 + .20) = 12 and min should be (10 * (1 - .20) = 8. However, if I am getting the error thrown by the damageFlux call, then it means they are most likely matching the same value.




Aucun commentaire:

Enregistrer un commentaire