0

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???

wuno
  • 9,547
  • 19
  • 96
  • 180

2 Answers2

3

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;
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
1

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;
   }
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Nice.. you beat me to it :) – int21h Jul 27 '14 at 00:17
  • Thanks a ton I can use this for each different character. If the 1.9 changes based on the character I am calculating I could just add the 1.9 to a variable in that characters class and just do *Foo there? Would that be the professional way to do that? – wuno Jul 27 '14 at 00:25
  • @NichoDiaz It depends on whether this value is constant across all characters of the same type (such as Warrior), or each character can have a different value. The former would imply a constant in the class, while the latter would imply a property in the CharacterType class, with a getter and setter. – Eran Jul 27 '14 at 00:31
  • I am missing something still. private rand = new Random() Where are we defining what Random() is? – wuno Jul 27 '14 at 00:54
  • @NichoDiaz `java.util.Random` class is part of the Java language. You should import it. – Eran Jul 27 '14 at 00:56
  • @NichoDiaz I forgot to declare the type. It should be `private Random rand = new Random()`. – Eran Jul 27 '14 at 00:58