Results 1 to 14 of 14
  1. #1
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default 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?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Generate a random number in the range of 0 - N then add/subtract an offest.

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Read all the methods of the Random class and see if there is a method that generates negative numbers.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What do you mean by 'genuine way'?

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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;

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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);
    }
    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
    Java Code:
    int value = rng.nextInt(10 - 5 + 1) + 5;
    Which would evaluate to anywhere from 5 to 9, where am I wrong here?
    Last edited by sunde887; 04-05-2011 at 07:26 AM.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  11. #11
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by sunde887 View Post
    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
    Java 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.

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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; 04-06-2011 at 04:54 AM.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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

  1. Math.random Question
    By noble in forum New To Java
    Replies: 21
    Last Post: 09-16-2010, 02:17 AM
  2. Replies: 17
    Last Post: 07-25-2010, 07:20 PM
  3. Replies: 5
    Last Post: 12-21-2009, 01:17 PM
  4. RTP question about random numbers
    By tornbacchus in forum New To Java
    Replies: 4
    Last Post: 04-14-2009, 03:58 AM
  5. math.random function help
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 03:31 AM

Tags for this Thread

Posting Permissions

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