Results 1 to 6 of 6
  1. #1
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Generating random numbers within an interval

    I've been trying to find the easiest way to write to generate a number which is between intervals of a arbitrary min and a max value.

    I've been searching for this but I don't find this particular thing.

    I've found that this combination works:

    Java Code:
    int guess = rand.nextInt(max - (min - 1)) + min;
    But I wonder, is this really the easiest way of writing it?
    Last edited by Stud1; 01-13-2014 at 09:32 PM.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Generating random numbers within an interval

    Your code may be wrong, but that depends exactly how you are defining min and max. If both ends of the range are inclusive, then you've written it correctly. As always, you can split up complex statements into multiple lines:

    Java Code:
    int range = max - min + 1; // omit +1 if max is exclusive
    int guess = min + rand.nextInt(range);
    Get in the habit of using standard Java naming conventions!

  3. #3
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    175
    Rep Power
    0

    Default Re: Generating random numbers within an interval

    Maybe I'm wrong and this isn't doable this way, but I'm pretty sure that if you read up on
    Java Code:
    math.random()
    That there is a way to do it. I seem to recall doing
    Java Code:
    int random = (int) math.random() * 45 + 10;
    or something like that to get a random number between 10 and 55?

  4. #4
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Generating random numbers within an interval

    Quote Originally Posted by kjkrum View Post
    Your code may be wrong, but that depends exactly how you are defining min and max. If both ends of the range are inclusive, then you've written it correctly. As always, you can split up complex statements into multiple lines:

    Java Code:
    int range = max - min + 1; // omit +1 if max is exclusive
    int guess = min + rand.nextInt(range);
    Yes both ends of the range should be inclusive. Thanks for clarifying.

    Just to clarify, does this work also for negative numbers? It seems it does when I try but I want to make sure.

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Generating random numbers within an interval

    It should. But it would not work if min > max.
    Get in the habit of using standard Java naming conventions!

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Generating random numbers within an interval

    s/math/Math

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Generating random numbers.
    By Zarquad in forum New To Java
    Replies: 1
    Last Post: 07-25-2013, 11:31 PM
  2. Generating random numbers
    By chinedum in forum New To Java
    Replies: 9
    Last Post: 09-21-2011, 05:03 AM
  3. I need help generating random numbers for a fill array
    By william979 in forum New To Java
    Replies: 15
    Last Post: 05-04-2011, 12:57 AM
  4. Powerball Example (Generating Random Numbers)
    By a7x75 in forum New To Java
    Replies: 8
    Last Post: 02-12-2010, 02:38 PM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 AM

Posting Permissions

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