Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with random class

 
angela brown
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
angela brown
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said

unfortunately I'm taking a data stuctures class

and Dirk said

Unfortunately, nothing.

He meant something like "Unfortunately, shmunfortunately."
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shoulda bin "unfortunately Nuthin'" said with grandiose swagger, surely.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, Vad -
Actually, I thought I was following Dirk and Ernest's clarification!
Maybe I need some time off...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic