# Double check my math?

Page 1 of 1

## 11 Replies - 584 Views - Last Post: 25 February 2015 - 01:00 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=369592&amp;s=384553268310419cdb37d423946a417f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Spiked Penguin

Reputation: -2
• Posts: 107
• Joined: 19-December 13

# Double check my math?

Posted 02 February 2015 - 01:49 AM

Quote

Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Unknown Source)
at com.hunt.game.Hunting110.Cow(Hunting110.java:30)
at com.hunt.game.HuntChances.HuntChance(HuntChances.java:20)
at com.hunt.game.Main.run(Main.java:31)

The program runs while the dmg is not negative but, when I calculate this... Even at level 1 the damage would be 1. So my random(atk)+1 is always going to be 1? I think... I am trying to avoid using doubles for convenience. If somebody could explain what is happening to return this error I would appreciate it.

Hunting110.java
```package com.hunt.game;

import java.util.Random;

public class Hunting110 {

Random rand = new Random();
int atk, dmg, def, exp, life, level;

public void Mouse(){
exp = rand.nextInt(7)+1; // Determines amount of xp.
level = rand.nextInt(2)+1; // determines enemy level 1 - 10
life = Player.level * 5 / 2 + level; // Calculates enemy health
atk = life / 2 * 3 / 4; // Calculates enemy atk.
def = atk * 4 / 5; // Calculates enemy def.
dmg = rand.nextInt(atk)+1; // Determines enemy dmg.

System.out.println("You have encountered a mouse");
System.out.println("Life: " + life);

}

public void Cow(){
exp = rand.nextInt(10)+1; // Determines amount of xp.
level = rand.nextInt(3)+1; // determines enemy level 1 - 10
life = Player.level * 5 / 2 + level; // Calculates enemy health
atk = life / 2 * 3 / 4; // Calculates enemy atk.
def = atk * 4 / 5; // Calculates enemy def.
dmg = rand.nextInt(atk)+1; // Determines enemy dmg.

System.out.println("You have encountered a Cow");
System.out.println("Life: " + life);

}

public void SewerRat(){
exp = rand.nextInt(10)+1; // Determines amount of xp.
level = rand.nextInt(4)+1; // determines enemy level 1 - 10
life = Player.level * 5 / 2 + level; // Calculates enemy health
atk = life / 2 * 3 / 4; // Calculates enemy atk.
def = atk * 4 / 5; // Calculates enemy def.
dmg = rand.nextInt(atk)+1; // Determines enemy dmg.

System.out.println("You have encountered a Sewer Rat");
System.out.println("Life: " + life);

}

public void Imp(){
exp = rand.nextInt(17)+1; // Determines amount of xp.
level = rand.nextInt(6)+1; // determines enemy level 1 - 10
life = Player.level * 5 / 2 + level; // Calculates enemy health
atk = life / 2 * 3 / 4; // Calculates enemy atk.
def = atk * 4 / 5; // Calculates enemy def.
dmg = rand.nextInt(atk)+1; // Determines enemy dmg.

System.out.println("You have encountered a mouse");
System.out.println("Life: " + life);

}

public void Goblin(){
exp = rand.nextInt(20)+1; // Determines amount of xp.
level = rand.nextInt(10)+1; // determines enemy level 1 - 10
life = Player.level * 5 / 2 + level; // Calculates enemy health
atk = life / 2 * 3 / 4; // Calculates enemy atk.
def = atk * 4 / 5; // Calculates enemy def.
dmg = rand.nextInt(atk)+1; // Determines enemy dmg.

System.out.println("You have encountered a Goblin");
System.out.println("Life: " + life);

}

}

```

HuntChances.java
```package com.hunt.game;

import java.util.Random;

public class HuntChances {

Random rand = new Random();
Hunting110 hunt110 = new Hunting110();
int chance;

public void HuntChance(){
// Level 1 - 10
if(Player.level < 10){
chance = rand.nextInt(5)+1;

switch(chance){
case 1:
hunt110.Mouse();
case 2:
hunt110.Cow();
case 3:
hunt110.SewerRat();
case 4:
hunt110.Imp();
case 5:
hunt110.Goblin();

}
}

// Level 11 threw 20
else if(Player.level > 10 && Player.level < 20){

}
}
}

```

This post has been edited by Spiked Penguin: 02 February 2015 - 01:50 AM

Is This A Good Question/Topic? 0

## Replies To: Double check my math?

### #2 TimHGMoon

Reputation: 24
• Posts: 190
• Joined: 04-September 14

## Re: Double check my math?

Posted 02 February 2015 - 02:08 AM

I don't think you should be using rand.nextInt(). The nextInt() is mainly used when you ask a user to input a number and you want the number that the user inputs. I think what you're trying to do requires Math.random().

### #3 CreamDelight

Reputation: 32
• Posts: 131
• Joined: 04-July 11

## Re: Double check my math?

Posted 02 February 2015 - 02:15 AM

The problem lies in this code.
```atk = life / 2 * 3 / 4;
```

At certain point, the value of atk became negative and you can't pass a negative value to nextInt.

### #4 Zavael

Reputation: 3
• Posts: 24
• Joined: 13-May 14

## Re: Double check my math?

Posted 02 February 2015 - 02:26 AM

CreamDelight pointed the line of code, but the problem is that atk can become 0 at lowest player level, not negative.
You should care closely at using multiplication or division with ints

### #5 CreamDelight

Reputation: 32
• Posts: 131
• Joined: 04-July 11

## Re: Double check my math?

Posted 02 February 2015 - 02:39 AM

Try to print out the value of your life, level and atk so you can check if what makes the expression return 0.

I tried evaluating using the least possible values of random in your code.

```level = rand.nextInt(2)+1;
```

level least value is 1
```life = Player.level * 5 / 2 + level;
```

Player.level value is 1
so life = 1 * 5 / 2 + level = 3.5
```atk = life / 2 * 3 / 4;
```

therefore atk = 3.5 / 2 * 3 / 4 = 1.3125

And atk should have the value of 1 at minimum given if Player.level is set to 1.

### #6 Spiked Penguin

Reputation: -2
• Posts: 107
• Joined: 19-December 13

## Re: Double check my math?

Posted 02 February 2015 - 02:46 AM

Lowest possible level of player and enemy: 1,1

life = 3.5 (4)
atk = 1.3125 (1)
def = 1.05

Math for life:

Quote

1 * 5
5 / 2
2.5 + 1
3.5

Math for atk

Quote

3.5 / 2
1.75 * 3
5.25 / 4
1.3125

Math for def

Quote

1.3125 * 4
5.25 / 5
1.05

Nowhere it became negative... This is the part I do not understand because, 1.05 is the lowest value and that would get rounded to 1.

EDIT:

@CreamDelight Looking at your code it seems you are running into the same problem as I am... should I just make an if saying that if atk =< 0 then atk = 1;

EDIT 2:

Or I guess I could do a try and catch then have it alter the atk if less than 0, That might be safer...

This post has been edited by Spiked Penguin: 02 February 2015 - 02:51 AM

### #7 Zavael

Reputation: 3
• Posts: 24
• Joined: 13-May 14

## Re: Double check my math?

Posted 02 February 2015 - 03:23 AM

you are wrong about math.. try this:

```        int n = 1*5/2+1;
System.out.println("n: " + n);
int p = n/ 2 * 3 / 4;
System.out.println("p: " + p);
Random random = new Random();
int nextInt = random.nextInt(p);
System.out.println("nextInt: " + nextInt);
```

even if result of first computation is 3,5, after assign to int it is 3, then the p is 0 and therefore the illegalArgumentException occurs

### #8 Spiked Penguin

Reputation: -2
• Posts: 107
• Joined: 19-December 13

## Re: Double check my math?

Posted 02 February 2015 - 03:36 AM

I tried to write it out and here is what I got:

```package mathTest;

import java.util.Random;

public class MathTester {

public static void main(String args[]){
Random rand = new Random();
int atk, dmg, def, exp, life, level;

exp = rand.nextInt(10)+1; // Determines amount of xp.
level = rand.nextInt(4)+1; // determines enemy level 1 - 10
life = 1 * 5 / 2 + level; // Calculates enemy health
atk = life / 2 * 3 / 4; // Calculates enemy atk.
def = atk * 4 / 5; // Calculates enemy def.
dmg = rand.nextInt(atk); // Determines enemy dmg.

System.out.println("exp: " + exp);
System.out.println("level: " + level);
System.out.println("Life: " + life);
System.out.println("Atk: " + atk);
System.out.println("Def: " + def);
System.out.println("Dmg: " + dmg);
}
}

```

Results

Quote

Level 1 is returning an error it appears

Level 2:

Quote

exp: 10
level: 2
Life: 4
Atk: 1
Def: 0
Dmg: 0

Level 3:

Quote

exp: 9
level: 3
Life: 5
Atk: 1
Def: 0
Dmg: 0

Level 4:

Quote

exp: 5
level: 4
Life: 6
Atk: 2
Def: 1
Dmg: 0

Not sure as to why def is 0 until level 4. Because according to a calculator it should be 1.35 (1). It doesn't seem to have a random feel />/>

```if(atk <= 0){
atk = 2;
}else
```

Prints out:

Quote

exp: 10
level: 1
Life: 3
Atk: 2
Def: 0
Dmg: 1

I think I found successful way to fix this problem. Not worried about the defense problem right now. Maybe this will be a bug I come to fix in the future but, for now I am ok with my enemy having 0 defense if they are level 1 - 3.

This post has been edited by Spiked Penguin: 02 February 2015 - 03:48 AM

### #9 Zavael

Reputation: 3
• Posts: 24
• Joined: 13-May 14

## Re: Double check my math?

Posted 02 February 2015 - 04:50 AM

I would set it to 1 instead of 2 but ist your code
if you would do the outputing right after assignment, you would see that the attack is 0 for level 1

```package mathTest;

import java.util.Random;

public class MathTester {

public static void main(String args[]){
Random rand = new Random();
int atk, dmg, def, exp, life, level;

exp = rand.nextInt(10)+1; // Determines amount of xp.
System.out.println("exp: " + exp);
level = rand.nextInt(4)+1; // determines enemy level 1 - 10
System.out.println("level: " + level);
life = 1 * 5 / 2 + level; // Calculates enemy health
System.out.println("Life: " + life);
atk = life / 2 * 3 / 4; // Calculates enemy atk.
System.out.println("Atk: " + atk);
def = atk * 4 / 5; // Calculates enemy def.
System.out.println("Def: " + def);
dmg = rand.nextInt(atk); // Determines enemy dmg.
System.out.println("Dmg: " + dmg);
}
}

```

right now i dont know why but doing
```        System.out.println("3/2*3/4: " + 3/2*3/4);
```

returns 0

### #10 Spiked Penguin

Reputation: -2
• Posts: 107
• Joined: 19-December 13

## Re: Double check my math?

Posted 02 February 2015 - 05:17 AM

I appreciate everybody helping. Using the if statement fixed the problem and now 5 hours later i have a fully functional game with a leveling system with infinite levels (going to make it 135), an exp system, stat points to spend for each level up, different encounters for every couple levels .

### #11 mike73

Reputation: 250
• Posts: 918
• Joined: 24-April 10

## Re: Double check my math?

Posted 02 February 2015 - 11:21 AM

You could clean up the if else statement by just saying:
```atk = Math.max(atk,2);
```

The reason:
```System.out.println("3/2*3/4: " + 3/2*3/4);
```

returns 0 is simple, since the values are ints, the fraction gets cut off.
3/2 = 1.5 = 1
1*3 = 3
3/4 = 0.75 = 0

This post has been edited by mike73: 02 February 2015 - 11:23 AM

### #12 Zavael

Reputation: 3
• Posts: 24
• Joined: 13-May 14

## Re: Double check my math?

Posted 25 February 2015 - 01:00 AM

mike73, on 02 February 2015 - 11:21 AM, said:

The reason:
```System.out.println("3/2*3/4: " + 3/2*3/4);
```

returns 0 is simple, since the values are ints, the fraction gets cut off.
3/2 = 1.5 = 1
1*3 = 3
3/4 = 0.75 = 0

YES! i knew that messing with division and ints has unpredictable results if someone does not think about the fractions