This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes random numbers, returns  a negative number? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "random numbers, returns  a negative number?" Watch "random numbers, returns  a negative number?" New topic
Author

random numbers, returns a negative number?

matthew christensen
Greenhorn

Joined: Sep 14, 2010
Posts: 17
hey guys im new to coderanch.

im having some problem wit this code. its generating a random number, but its negative and i cant figure out why. im writing a text-based-rpg-combat-system.

it gets its variables from this class

not sure what the big deal is, no compiler errors come up
im using netbeans 6.9.1 and jdk 6_21
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11443
    
  16

not sure why you say you're getting negative numbers...taking the least amount of your code as possible:

I didn't get a negative value once.

I'd suggest putting more S.o.p.'s in your code and see what it's really doing.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Agreed; there's no way the random number itself is negative.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
. . . and welcome to the Ranch
Mohamed Omar
Greenhorn

Joined: Sep 28, 2010
Posts: 2
sorry if this is a late reply, I saw this post just now.

the problem with your code is that your subtracting the dmg (which is already negative) from AIhp resulting an increase in the AIhp. you should change it to


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Mohamed Omar, welcome to JavaRanch And thank you for finally finding the error
Mohamed Omar
Greenhorn

Joined: Sep 28, 2010
Posts: 2
Your welcome
 
wood burning stoves
 
subject: random numbers, returns a negative number?