Results 1 to 10 of 10
  1. #1
    chinedum is offline Member
    Join Date
    Sep 2010
    Posts
    1
    Rep Power
    0

    Default Generating random numbers

    Good day people,pls i need help developing a subroutine of function that generates 15 random integer numbers between 0 AND 75,but the caught is that once a number has been generated ,e.g 2 ,that number is never repeated or generated again,if it is the subroutine calls math.random until it generates a number that hasnt been generated before.How do i do this,do i create variables to hold each generated integer and then test that value against the new generated interger...pls i need help with the code

  2. #2
    XmisterIS is offline Member
    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    0

    Default

    Off the top of my head, you could store the numbers in a vector and check the vector in a while loop each time you want to generate a new number (e.g. "while number exists in vector generate another"). You could start the while loop by initially assigning the first element of the vector to your random number, so you don't have two calls to math.random (messy, IMHO!)

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    You only have 75 numbers (76 if it's inclusive) so...

    Build a single List containing all those numbers.

    Then list.delete() one using a random number 0 to list length for the index. Store the result of the delete in your results List.
    Do this 15 times.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    You only have 75 numbers (76 if it's inclusive) so...

    Build a single List containing all those numbers.

    Then list.delete() one using a random number 0 to list length for the index. Store the result of the delete in your results List.
    Do this 15 times.
    Don't forget the Collections.shuffle( ... ) method; as in:

    Java Code:
    public List rand15() {
       List<Integer> list= new ArrayList<Integer>();
       for (int i= 0; i < 75; list.add(i++));
       return Collections.shuffle(list).subList(0, 15);
    }
    kind regards,

    Jos

  5. #5
    siva_vs_siva is offline Member
    Join Date
    Aug 2010
    Posts
    31
    Rep Power
    0

    Default

    import java.util.ArrayList;
    import java.util.Random;
    public class Generate {
    public static void main(String args[])
    {
    int count=0,gen;
    ArrayList array=new ArrayList();
    Random random=new Random();

    while(count!=15)
    {
    gen=random.nextInt(100);
    if(gen>0&&gen<=75)
    {
    if(!array.contains(gen))
    {
    array.add(gen);
    System.out.println(gen);
    count++;
    }
    }
    }
    }

    }

  6. #6
    siva_vs_siva is offline Member
    Join Date
    Aug 2010
    Posts
    31
    Rep Power
    0

    Default

    // May be this also good

    import java.util.ArrayList;
    import java.util.Random;
    public class Generate {
    public static void main(String args[])
    {
    int count=0,gen;
    ArrayList array=new ArrayList();
    Random random=new Random();

    while(count!=15)
    {
    gen=random.nextInt(75); //up to 75
    if(!array.contains(gen))
    {
    array.add(gen);
    System.out.println(gen);
    count++;
    }

    }
    }

    }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    Don't forget the Collections.shuffle( ... ) method; as in:

    Java Code:
    public List rand15() {
       List<Integer> list= new ArrayList<Integer>();
       for (int i= 0; i < 75; list.add(i++));
       return Collections.shuffle(list).subList(0, 15);
    }
    kind regards,

    Jos
    Curses!
    I always forget shuffle()!

    I have seen that so many times you'd think I'd remember it by now...:)

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    Arrgh!
    I'm stuck in an infinite loop!

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

Similar Threads

  1. Powerball Example (Generating Random Numbers)
    By a7x75 in forum New To Java
    Replies: 8
    Last Post: 02-12-2010, 02:38 PM
  2. Replies: 12
    Last Post: 09-23-2009, 09:14 AM
  3. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  4. Generating a random number
    By oridov in forum New To Java
    Replies: 2
    Last Post: 11-29-2008, 05:12 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

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
  •