aspose file tools*
The moose likes Beginning Java and the fly likes Help with random class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with random class" Watch "Help with random class" New topic
Author

Help with random class

angela brown
Greenhorn

Joined: Oct 07, 2003
Posts: 4
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?
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
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...)
angela brown
Greenhorn

Joined: Oct 07, 2003
Posts: 4
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
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
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!
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Dirk -
I'm curious: What were you responding to when you wrote, "Unfortunately, nothing." It seems like a non-sequitur to me, but maybe I missed something (and no, that's not a pun)...
Thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

You said

unfortunately I'm taking a data stuctures class

and Dirk said

Unfortunately, nothing.

He meant something like "Unfortunately, shmunfortunately."


[Jess in Action][AskingGoodQuestions]
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Here's another solution working on a bit-wise principle - it chops off the left bits beyond the required range of the random numbers:
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
I didn't say "unfortunately I'm taking"; that was Angela.
But you have cleared my fog regarding Dirk's response.
Thanks.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
But you have cleared my fog regarding Dirk's response.
I am still kinda foggy. Can you explain it to me?
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Eugene -
It seems he was saying "Not unfortunate, but fortunate that you're taking the course, and that you have JavaRanch folks to help.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
You might disagree, but I believe what Dirk really meant in the first place was very close to Ernest's clarification although pretty well disguised.
Jeremy Thornton
Ranch Hand

Joined: Feb 21, 2002
Posts: 91
Shoulda bin "unfortunately Nuthin'" said with grandiose swagger, surely.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hey, Vad -
Actually, I thought I was following Dirk and Ernest's clarification!
Maybe I need some time off...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with random class
 
Similar Threads
Preventing Divide by zero exception
Please Help
array of random numbers
Noob computer science student.
converting ascii number to capital letter