# Random Function Question.

• 04-05-2011, 06:50 AM
Dark
Random Function Question.
As I was going through my Java book it brought me upon java.util.Random. Though it only allows me to generate positive numbers. Is there a way to generate negative numbers?

I was trying to accomplish a range of -1 to 1, which isn't a huge range and I could achieve the results by other means of assigning variables based off of the number it generated from the range of myVariable.nextInt(3) though that would be horribly inefficient. Especially if my range was larger than just 3. Is there a way to accomplish this?
• 04-05-2011, 06:56 AM
Junky
Generate a random number in the range of 0 - N then add/subtract an offest.
• 04-05-2011, 07:05 AM
Dark
Ok, is this the only method of accomplishing my task? Is there no real way to generate a negative number?

Thanks for the advice. I will use it but just for knowledge I am still interested in seeing if there is a genuine way to generate a negative random number.
• 04-05-2011, 07:07 AM
Junky
Read all the methods of the Random class and see if there is a method that generates negative numbers.
• 04-05-2011, 07:08 AM
r035198x
What do you mean by 'genuine way'?
• 04-05-2011, 07:11 AM
sunde887
there is no genuine way that I can think of, it simply generates a number from 0 - x.

You can also multiply the result by -1.
• 04-05-2011, 07:15 AM
Junky
Actually there is no "genuine" way to generate a random number. They are called pseudo random number generators for a reason.

No need to multiply by -1. Use the following formula for any situation:
Code:

`int value = rng.nextInt(high - low + 1) + low;`
• 04-05-2011, 07:16 AM
Dark
Well looking through the Random method the answer is no. Using java.util.Random you can only get a positive Pseudorandom number. I think my question may have been misunderstood because I was asking if there was any way to accomplish it in Java at all.

So I consulted with my highly intelligent partner Google and stumbled across that the java.lang.Math has a rnd.nextInt that might be able to do what I'm looking for but I'm not sure yet. I have to do some more reading.

@Sunde887
If you multiply it by -1 then wouldn't all my random numbers be negative? This doesn't help me if I need a negative low and a positive high of my range.
• 04-05-2011, 07:22 AM
sunde887
Ya, it would only produce negative numbers. Junky has given you a simpler solution, however you could do something like
Code:

```int val; Random random = new Random(); if(Math.random() < .5){   val = random.nextInt(100) * -1; } else{   val = random.nextInt(100); }```
Junky, for some reason the snippet you showed doesn't seem to work to me. Mind explaining?

if high is 10, and low is 5 then it would do
Code:

`int value = rng.nextInt(10 - 5 + 1) + 5;`
Which would evaluate to anywhere from 5 to 9, where am I wrong here?
• 04-05-2011, 07:22 AM
Junky
No your answer was not misunderstood. By getting you to read the API for the Random class I was hoping you would come to the conclusion that there is no way. Unless you want to spend years developing and refining your own algorithm to generate random numbers. A simple bit of research into the subject will quickly reveal that this is not an easy task to achieve. Just use the Random class and the formula I provide and get on with it.
• 04-05-2011, 08:55 AM
Dark
I came across that formula you posted when I was searching which was cool, but theres no reason why continued research is a bad thing. Besides I found a couple of cool things I could use in the future during my researching.

Thanks for the help guys.

Apparently I can't mark my own topics as solved. If a moderator could mark this as solved I would be most grateful.
• 04-06-2011, 04:27 AM
Junky
Quote:

Originally Posted by sunde887
Junky, for some reason the snippet you showed doesn't seem to work to me. Mind explaining?

if high is 10, and low is 5 then it would do
Code:

`int value = rng.nextInt(10 - 5 + 1) + 5;`
Which would evaluate to anywhere from 5 to 9, where am I wrong here?

How are you testing it. If you place that inside a loop that iterates 50+ times you should get all values displayed at least once.
• 04-06-2011, 04:47 AM
sunde887
I put it in a loop and tested if the result was less than 0, if it was it would print that there is a negative, ran it 1000000000 times and didn't find anything negative.

Code:

```Random rand = new Random(); for(int i = 0; i < 1000000000; i++){   int value = rand.nextInt(10 -  5 + 1) + 5;   if(value < 0){     System.out.println("value " + value + " is negative");   }  }```
• 04-06-2011, 04:51 AM
Junky
Of course you wont get any negative values. The range is 5 to 10. If you want random negative values then the low value has to be negative.
Code:

```int high = 10; int low = -5; Random rng = new Random(); for(int index = 0; index < 50; index++) {     System.out.println(rng.nextInt(high - low + 1) + low); }```