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

random numbers

Shalvin Maharaj
Greenhorn

Joined: Nov 06, 2001
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
Help please
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
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

Joined: Oct 31, 2001
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

Joined: Oct 16, 2001
Posts: 35
Hi Shalvin,
Please try out this code:

I've tried this and this is working fine.
Kris Nelson
Ranch Hand

Joined: Nov 04, 2001
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


WebNelly.com<br />Java/XML Web Development<br />Check it out!<br /><a href="http://www.webnelly.com" target="_blank" rel="nofollow">http://www.webnelly.com</a>
Kris Nelson
Ranch Hand

Joined: Nov 04, 2001
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

Joined: Oct 16, 2001
Posts: 35
no problem Kris, better luck next time
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
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

Joined: Sep 23, 2001
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


Michael J Bruesch<br /><i>I code, therefore I am.</i>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: random numbers