File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 10916
    
  12

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: 36514
    
  16
. . . 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: 36514
    
  16
Mohamed Omar, welcome to JavaRanch And thank you for finally finding the error
Mohamed Omar
Greenhorn

Joined: Sep 28, 2010
Posts: 2
Your welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: random numbers, returns a negative number?
 
Similar Threads
Minimum and maximum random number?
Random Generation of Shapes
Conceptual Misunderstanding of Arraylists?
Question About Inheritance
using an applet to open another window (JFrame)