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

random numbers

 
Shalvin Maharaj
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a problem doing an assignment. Our teacher asked us to genenrate random numbers between -100 and 100 and I have no idea how to generate a negative number let alone negative hundered
Help please
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure how you would do that, other than perhaps creating a random number from 0 to 100, like you normally would, and then using random boolean and an if statement to decide if you should multiply that random number by -1.
That's probably how I would do it, since can't seem to find anything in the API that would do what you want.
Jason

[This message has been edited by jason adam (edited November 06, 2001).]
 
David Eason
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Random Numbers from Maha's Java Gotchas page.

[This message has been edited by David Eason (edited November 06, 2001).]
 
Argm Mastoi
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shalvin,
Please try out this code:

I've tried this and this is working fine.
 
Kris Nelson
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably the easiest way to create a random number between 0 and 200, then subtract 100:
int n = (int)(Math.random() * 200) - 100;
This way, if the random number is greater than 100 it will be positive and if it is less than 100, it will be negative. Here are some example values:
Random number
from 0 - 200
-------------
25 - 100 = -75
50 - 100 = -50
125 - 100 = 25
150 - 100 = 50

------------------
WebNelly.com
Java/XML Web Development
Check it out!
http://www.webnelly.com
 
Kris Nelson
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the timestamps, Argm beat me by one minute.
Great typing Argm.

------------------
WebNelly.com
Java/XML Web Development
Check it out!
http://www.webnelly.com
 
Argm Mastoi
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no problem Kris, better luck next time
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now why was I thinking that you could only add to the random number you get after you multiply your range? Probably because that's how I've used it everytime. Goes to show what continuous lack of quality sleep does to you.
Jason
 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hehe...sleep is good. Yeah, we had this conversation before, and we came up with a formula:
int randX = (int) (Math.random () * (totalRange + 1) + startPoint)
Your startPoint would be -100, therefore you'd subtract 100 there. Your totalRange would be 201 numbers (from -100 to 100 including 0). Then you add one to that to include the rangeMax. For some reason, the Math.random () function gives you a random number between 0.0 inclusive and 1.0 exclusive. So simply add 1 and it will take care of the maximum value in your range. So by this method I think your expression would be what Argm stated (he was part of that conversation ) only your totalRange would be 202. I think...
int randX = (int) (Math.random () * 202 - 100);

------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic