Cacoon
Cacoon

Reputation: 2538

Generating attack for java game

I am creating an 'advanced' text adventure game on java to get into learning more about class useage and whatnot. I am trying to work out the logic of attacking and this is what I have so far:

What I want to do is have another random smaller multiplier for different things, like intel is a stat the character will have, what would be the best way to add a multiplyer?

public int magicAttack()
    {
        int randomMultiplyer = randomGenerator.nextInt(10);
        double extraMultiplyer = randomGenerator.nextDouble();
        System.out.println((character.basedmg/*10*/ + character.strength) * randomMultiplyer);
        return (character.basedmg/*10*/ + character.magic) * randomMultiplyer;
    }

Here are the classes Im using Character.java and AttackManager.java

So with the above code that I pasted here, I dont want to be overly technical with how it works, simply that it works reasonably well. I want to add another multiplyer (which I have started as you can see from the double 'extraMultiplyer' and I wanted it to maybe just add 0.6, so multiplying it by 1.6 but I am not sure how to get that with random.

Upvotes: 0

Views: 356

Answers (1)

dting
dting

Reputation: 39287

If you are using Java utils.Random, nextDouble() returns a value between 0.0 and 1.0.

Multiplying by the difference of your desire max and min value (range) and adding your min value, you will get you a value between min and max.

Random r = new Random();
double multiplier = (max - min) * r.nextDouble() + min;

If you want 0 to 0.6,

double multiplier = 0.6 * r.nextDouble();

Upvotes: 1

Related Questions