This question already has an answer here:
-
How do I generate random integers within a specific range in Java?
65 answers
This is not just about finding a random number,
The answer I am looking for is how to add an expression to a random number to calculate a number based on the expression.
For example a base random number from a range of numbers,
+ the current lvl of a character,
* the character's damage gain.
If I am creating a character that does damage between two numbers like this in java
,
Damage = 44 to 55 per hit
and I want to write this into a variable, how can I do this?
I am trying this,
double BottomDamage = 44 + level * 1.9;
double TopDamage = 55 + level * 1.9;
What is the proper way to write something like this if the damage would be between these two numbers 44 through 55 + level * 1.9
???
You should write an interface with a method damage
and each type of character would have a class that implements this interface.
public interface CausesDamage ()
{
public double damage ();
}
public abstact class CharacterType implements CausesDamage ()
{
private int _level;
...
public abstract double damage ();
...
public int getLevel ()
{
return _level;
}
}
public class Warrior extends CharacteType ()
{
public static final int MIN_DAMAGE = ..;
public static final int MAX_DAMAGE = ..;
...
private Random rand = new Random();
...
public double damage ()
{
return rand.nextInt(MAX_DAMAGE-MIN_DAMAGE+1)+MIN_DAMAGE + getLevel()*1.9;
}
}
So a random number between the range 44 and 55?
Could be like this:
/* returns random number between min (inclusive) and max (inclusive) */
public static int randomInt(int min, int max) {
return min + (int)(Math.random() * (max - min + 1));
}
int damageRange = randomInt(44, 55);
int damage = damageRange + level * 1.9;