Results 1 to 14 of 14
Thread: Random Function Question.
 04052011, 07:50 AM #1
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?
 04052011, 07:56 AM #2
Generate a random number in the range of 0  N then add/subtract an offest.
 04052011, 08:05 AM #3
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.
 04052011, 08:07 AM #4
Read all the methods of the Random class and see if there is a method that generates negative numbers.
 04052011, 08:08 AM #5Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 14
What do you mean by 'genuine way'?
 04052011, 08:11 AM #6
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,068
 Blog Entries
 3
 Rep Power
 14
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.
 04052011, 08:15 AM #7
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:
Java Code:int value = rng.nextInt(high  low + 1) + low;
 04052011, 08:16 AM #8
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.
 04052011, 08:22 AM #9
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,068
 Blog Entries
 3
 Rep Power
 14
Ya, it would only produce negative numbers. Junky has given you a simpler solution, however you could do something like
Java Code:int val; Random random = new Random(); if(Math.random() < .5){ val = random.nextInt(100) * 1; } else{ val = random.nextInt(100); }
if high is 10, and low is 5 then it would do
Java Code:int value = rng.nextInt(10  5 + 1) + 5;
Last edited by sunde887; 04052011 at 08:26 AM.
 04052011, 08:22 AM #10
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.
 04052011, 09:55 AM #11
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.
 04062011, 05:27 AM #12
 04062011, 05:47 AM #13
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,068
 Blog Entries
 3
 Rep Power
 14
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.
Java 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"); } }
Last edited by sunde887; 04062011 at 05:54 AM.
 04062011, 05:51 AM #14
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.
Java 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); }
Similar Threads

Math.random Question
By noble in forum New To JavaReplies: 21Last Post: 09162010, 03:17 AM 
Java Question Need Help Restarting the Values of Variables from Random Numbers So Out
By JavaStudent1990 in forum New To JavaReplies: 17Last Post: 07252010, 08:20 PM 
hard question .. help me .. about creating random numbers
By soldier in forum New To JavaReplies: 5Last Post: 12212009, 02:17 PM 
RTP question about random numbers
By tornbacchus in forum New To JavaReplies: 4Last Post: 04142009, 04:58 AM 
math.random function help
By katie in forum New To JavaReplies: 2Last Post: 08062007, 04:31 AM
Bookmarks