I need to generate a random number between -99 to 99, how do I code this? This is what I have so far, but it always comes out negative Random gererator=new Random(); num1=Math.abs(generator.nextInt()) % 99 + -99; How do I fix it?
Hi, Angela - Welcome to JavaRanch. I noticed that your code has some typos which will prevent it from compiling. Once that's fixed, if you take modulus 199 (which gives you range of 199 ints), then subtracting 99, you'll get what you're looking for.
Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Joined: Oct 07, 2003
Thanks for responding, it's working now I'm a little rusty with Java and unfortunately I'm taking a data stuctures class that uses Java, so I may have more questions
angela missomething, Welcome to JavaRanch! We don't have many rules here, but the one we do have is a naming policy. Please edit your display name to comply with this policy. Thanks in advance, and we look forward to seeing you around the Ranch!
Originally posted by angela missomething: Thanks for responding, it's working now I'm a little rusty with Java and unfortunately I'm taking a data stuctures class that uses Java, so I may have more questions
Unfortunately, nothing. You couldn't be luckier, because you've got the Ranch to help you!