Results 1 to 5 of 5
  1. #1
    Michailangelo is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default Random number generator

    Hello,

    I am looking for a number generator that whenever selects a number, this number would no longer be available for reselection.
    Ultimately, this number generator will eventually select all the numbers from the range you define.

    I looked Random class but it doesn't do anything like this.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Michailangelo View Post
    I looked Random class but it doesn't do anything like this.

    clear, because this is an easy task for java programmers. build a collection for your drawing. after the first number was selected and added to the collection each further number is send to a method that returns a boolean true if the number is already in your collection or false if you can add it to your collection. with a while-loop you can loops till all drawings contains different numbers. you can put all your methods in a class and build a constructor that takes the range of numbers and the number of drawings.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    On j2me64's select-check-remember method, the Set class has an add() method that returns a boolean. (true iff the value is "good", ie you haven't seen it before).

    Another method, useful when the range from which the values is drawn is not too large. might be described as the shuffle-draw method. You take a List of some sort and fill it with all of the possible values you might select. Then you use the static Collections.shuffle() method. The random sequence now exists and you can draw the elements you want one at a time from the list.

  4. #4
    Michailangelo is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by j2me64 View Post
    with a while-loop you can loops till all drawings contains different numbers.
    I tried this and works. I thought that there is a better way to do it since you can not predict when the numbers will stop repeating.

    Anyway thank you both for your help.
    Last edited by Michailangelo; 04-02-2010 at 06:17 PM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by j2me64 View Post
    clear, because this is an easy task for java programmers. build a collection for your drawing. after the first number was selected and added to the collection each further number is send to a method that returns a boolean true if the number is already in your collection or false if you can add it to your collection. with a while-loop you can loops till all drawings contains different numbers. you can put all your methods in a class and build a constructor that takes the range of numbers and the number of drawings.
    There are drawbacks to that approach; as a matter of fact there are drawbacks to both or all approaches.

    Approach #1: generate random numbers and check if they have been drawn before. Suppose you have to draw them in the interval [1 ... 1000]. After you've sucessfully drawn 999 numbers you have to draw 1000 more numbers (on average) to find or draw that last number. Attempting to draw all 1000 numbers successfully (on average) you have to draw 1+2+3+ ... +1000 numbers == 1001*1000/2 which is a lot.

    Approacht #2: store all numbers [1 ... 1000] in a Collection and shuflle them. Afterwards you can draw them one by one. The disadvantage is clear: you need a collection of all 1000 numbers.

    If you want to draw m numbers from a set of n numbers where m << n (<< is much less than) the first approach will do fine, otherwise I prefer the second approach if n is not too large.

    kind regards,

    Jos

Similar Threads

  1. Mersenne twister random generator
    By mental in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:21 AM
  2. Help with class project, random number generator.
    By Christopher The Great in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 02:12 AM
  3. Random Shape generator
    By scheng12 in forum New To Java
    Replies: 1
    Last Post: 03-09-2009, 02:06 AM
  4. Replies: 3
    Last Post: 05-27-2008, 01:51 PM
  5. Random Generator
    By padutch2 in forum New To Java
    Replies: 1
    Last Post: 12-03-2007, 06:43 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
  •