1. Member
Join Date
Nov 2012
Posts
1
Rep Power
0

## Random number

Hello.
Im new to java and I have a small question, couldn't find answer to it.

How I can create a random number between -1000 and 1000 for example.
I guess it's possible to do it with Math.random() function, but how?

2. ## Re: Random number

Originally Posted by _Chip
Hello.
Im new to java and I have a small question, couldn't find answer to it.

How I can create a random number between -1000 and 1000 for example.
I guess it's possible to do it with Math.random() function, but how?

Also have a look at the Random class; note that if you can generate a pseudo random number in the interval [0 ... n) you can also generate a pseudo random number in the range [-m ... n-m) (simply subtract m from the first result).

kind regards,

Jos

3. ## Re: Random number

You can use the Random class which has a method called nextInt(range) which returns a number from 0 to range - 1. Just use the appropriate range, and then subtract 1000 from the returned int.

4. ## Re: Random number

Man, do I feel fast today.

kind regards,

Jos ;-)

5. ## Re: Random number

Originally Posted by JosAH
Man, do I feel fast today.
edit: I will try to behave today...
Last edited by Fubarable; 11-03-2012 at 07:27 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•