Results 1 to 7 of 7
  1. #1
    busdude is offline Member
    Join Date
    Oct 2008
    Posts
    25
    Rep Power
    0

    Default restrictions on randomly generated values?

    Is the a command in Java where you can disallow certain numbers from the range to be obtained with the Random class?


    So if you want to generate 5 random integers between 1-50, but you do not want any of them to be 15 or 20, for example, is there a way to block them or do you have to make some sort of loop where you get a new number if one of the disallowed ones is obtained?

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

    Default

    I think a simple loop would be the way to go here.

  3. #3
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    What Fubarable says, just use a while loop. (no for)
    I die a little on the inside...
    Every time I get shot.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    Quote Originally Posted by busdude View Post
    Is the a command in Java where you can disallow certain numbers from the range to be obtained with the Random class?


    So if you want to generate 5 random integers between 1-50, but you do not want any of them to be 15 or 20, for example, is there a way to block them or do you have to make some sort of loop where you get a new number if one of the disallowed ones is obtained?
    For your example, I would do as the others have suggested, and just have a loop which generates the number inside it and repeats until that number is neither 15 nor 20.

    However, to take the general case, for me it would really depend on the circumstances; i.e. whether the code is part of an algorithm that has to be very fast, and what proportion of the range was excluded.

    For example, if your range was 1-50, inclusive, but you wanted to exclude numbers in the range 5-44, then I would probably take a different approach. in this case, since there are only 10 permitted values then I would generate a random number between 1-10 and use a calculation to map this number onto the allowed values.

    To take another example, suppose you wanted to exclude odd numbers, then I would simply generate a number between 1-25 and double it.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    For the first suggestion; my question is how to map numbers into the allowed values. I'm not clear that how you going to do it.

    For the second; I don't think it's a good choice actually. If you double the value, it's not a random value in sense, is it? Because you generating the second number, but effect with the initial value.

  7. #7
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    For the first suggestion; my question is how to map numbers into the allowed values. I'm not clear that how you going to do it.

    For the second; I don't think it's a good choice actually. If you double the value, it's not a random value in sense, is it? Because you generating the second number, but effect with the initial value.
    busdude was asking a general question, but using a specific example, as I said in this example a loop would be the best way, but I give two other examples where I think it would not.

    In my first example, let me make it clear how you would do the mapping:

    Java Code:
    Random r = new Random();
    int random = r.nextInt(10);
    int mapped = (random<=3) ? random+1 : random + 41;
    System.out.println(mapped);
    And what it does is this:

    random-->mapped
    0-->1
    1-->2
    2-->3
    3-->4
    4-->45
    5-->46
    6-->47
    7-->48
    8-->49
    9-->50


    In my second example, I said that odd numbers have to be excluded.

    Java Code:
    Random r = new Random();
    int random = r.nextInt(25);
    int mapped = (random+1)*2
    And it maps like this:

    random-->mapped
    0-->2
    1-->4
    ......
    23-->48
    24-->50

    By definition, if any numbers are excluded then the distribution is not uniform.

Similar Threads

  1. Replies: 1
    Last Post: 09-29-2008, 09:45 PM
  2. Replies: 0
    Last Post: 05-22-2008, 10:53 AM
  3. Replies: 4
    Last Post: 04-15-2008, 01:04 PM
  4. getting dynamically generated valus
    By abhiN in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-29-2008, 10:58 AM
  5. passing dynamically generated values
    By abhiN in forum Advanced Java
    Replies: 1
    Last Post: 01-20-2008, 03:21 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
  •