programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# random numbers

Greenhorn
Posts: 1
• Number of slices to send:
Optional 'thank-you' note:
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 ()
Posts: 1932
• Number of slices to send:
Optional 'thank-you' note:
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).]

Greenhorn
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:
Check out Random Numbers from Maha's Java Gotchas page.

[This message has been edited by David Eason (edited November 06, 2001).]

Ranch Hand
Posts: 35
• Number of slices to send:
Optional 'thank-you' note:
Hi Shalvin,

I've tried this and this is working fine.

Ranch Hand
Posts: 35
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
no problem Kris, better luck next time

Chicken Farmer ()
Posts: 1932
• Number of slices to send:
Optional 'thank-you' note:
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

Ranch Hand
Posts: 158
• Number of slices to send:
Optional 'thank-you' note:
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

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?