This week's book giveaway is in the Performance forum.We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!

random numbers

Shalvin Maharaj
Greenhorn
Posts: 1
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

Chicken Farmer ()
Ranch Hand
Posts: 1932
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
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
Hi Shalvin,

I've tried this and this is working fine.

Kris Nelson
Ranch Hand
Posts: 35
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
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
no problem Kris, better luck next time

Chicken Farmer ()
Ranch Hand
Posts: 1932
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
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