Results 1 to 9 of 9
  1. #1
    frasifrasi is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default How do I generate random numbers in a certain range using the random class?

    I know how to use Math.random for this, but how would I generate random numbers using the random class?

    Say I want a number between 40 and 50, inclusive--how would I do this?

    What i have in mind is:

    int randomNumber = random.nextInt(max) + min;

    where max is 50 and min is 40. Is this correct?

    Thank you.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Random numbers always go from 0 to the max. And you know that you want to start with 40 and add a random number to it. So how do you do this? Think on this and you will figure this out.

  3. #3
    frasifrasi is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default

    I have searched it for the past 30 min and did not find an exact method for int. Can anyone help me out here? I need this before proceeding with my program.

  4. #4
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    use else/if statement to filter the undesired generated integer number....
    freedom exists in the world of ideas

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.Random;
    
    public class Test {
        public static void main(String[] args) {
            Random seed = new Random();
            // range [40 - 50]
            int base = 40;
            int variable = 10;
            int min =  Integer.MAX_VALUE;
            int max = -Integer.MAX_VALUE;
            for(int i = 0; i < 100; i++) {
                // 0 <= range returned < specified value
                int next = 40 + seed.nextInt(variable + 1);
                if(next < min) min = next;
                if(next > max) max = next;
            }
            System.out.printf("min = %d  max = %d%n", min, max);
        }
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by frasifrasi View Post
    I have searched it for the past 30 min and did not find an exact method for int. Can anyone help me out here? I need this before proceeding with my program.
    You don't need an "exact method". We've given you enough hints, now all you need is a functioning brain.

  7. #7
    frasifrasi is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default

    Ok, thanks. I searched extensively and didn't come across anything.

  8. #8
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    rand(max - min) + min.
    My IP address is 127.0.0.1

  9. #9
    DavidG24 is offline Member
    Join Date
    Apr 2009
    Posts
    49
    Rep Power
    0

    Default

    hey frasifrasi,

    the method you have imposed is almost perfect,

    if you wish to generate a random integer in the inclusive range a, b you must simply use the pseducode

    rand = a + randominteger(b-a)

    In Java code this becomes

    Random R = new Random() //create Random Object
    int random_number = min + R.nextInt(max - min + 1) //The reason you need the +1 is that nextInt(n) generates a random int from 0 inclusive to n exclusive, thus to generate from 0 to n, simply use nextInt(n + 1)

    Hope this helps,

    David
    Last edited by DavidG24; 04-19-2009 at 07:04 PM.

Similar Threads

  1. Random numbers
    By jithan in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2008, 03:04 PM
  2. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM
  3. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 03:56 AM
  4. Generate a random number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:23 AM
  5. How to generate random number in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 08:32 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
  •