CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2009
    Posts
    52

    Random Number Generator help!

    Hi i am trying to print 6 numbers randomly..however i do not want any of the numbers to be a duplicate. Could you help me see what is wrong with my codes/flow? as i am still getting duplicates. Thanks in advance.


    Code:
    boolean donPrintFlag = false;
    //create random object to use
                Random r2 = new Random();
    
                //create array object
                int arrayNum[] = new int[7];
    
    
                for (int i = 0; i < 6; i++) {
                    int randNum3 = r2.nextInt(45) + 1;
    
                    //initialise 6 numbers into array
                    arrayNum[i] = randNum3;
                    //  System.out.println("array print " + arrayNum[i]);
    
                    //loop through all numbers to find duplicate
                    for (int j = 1; j < 6; j++) {
                        if (arrayNum[i] == arrayNum[j]) {
                            donPrintFlag = true;
                        }
                        else{
                            donPrintFlag = false;
                        }
                    }
    
                    //print non duplicate numbers
                    if (!donPrintFlag) {
                        System.out.println("Number" + i + ": " + arrayNum[i]);
                    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Random Number Generator help!

    There are several ways of doing this, such as:

    • If there aren't many numbers: fill an ArrayList with the numbers (ie all the numbers from 1 to 10) and call the Collections.shuffle() method to randomise the list. Finally pull out the first 'n' numbers.
    • Create a Set and fill it with random numbers until it's size is the required size (ie 6), then use an iterator to extract the numbers.
    • Or, if you really have to use an array, check the number is not already in the array before you add it.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Random Number Generator help!

    Alternatively, generate random numbers and add them to a Set (e.g. HashSet). A Set guarantees no duplicates.

    The ability to simplify means to eliminate the unnecessary so that the necessary may speak...
    H. Hofmann
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Random Number Generator help!

    Alternatively, generate random numbers and add them to a Set (e.g. HashSet). A Set guarantees no duplicates.
    Err isn't using a Set what I suggested in my second point?

    Although with a HashSet you may lose some of the randomness, depending on how the numbers get distributed into the buckets and the order in which they are returned by the iterator. Using a LinkedHashset maintains the generation order so probably should be used instead.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Random Number Generator help!

    Quote Originally Posted by keang View Post
    Err isn't using a Set what I suggested in my second point?
    Yes, sorry - I was in a rush, speed-reading the thread

    Although with a HashSet you may lose some of the randomness, depending on how the numbers get distributed into the buckets and the order in which they are returned by the iterator. Using a LinkedHashset maintains the generation order so probably should be used instead.
    That's an interesting point - I wonder if it does make a difference... but as you say, LinkedHashSet would be the safer bet.

    The one who insists on never uttering an error must remain silent...
    W. Heisenberg
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Random Number Generator help!

    That's an interesting point - I wonder if it does make a difference.
    The reason I suggested it maybe an issue is as follows:

    Assume you generate 2 series of random numbers which happen to contain the same numbers but in a different order eg 7, 98, 68, 27 and 27, 98, 7, 68. If each number happens to be in a different HashSet bucket then both series will be returned as identical sequences by the iterator eg 27, 98, 68, 7.

    I've tested this to prove it on my system and these numbers do get reordered to the same sequence.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Random Number Generator help!

    Quote Originally Posted by keang View Post
    I've it on my system and these numbers do get reordered to the same sequence.
    OK, I'convinced

    Good thinking. I must be getting old

    Being abstract is something profoundly different from being vague... The purpose of abstraction is not to be vague, but to create a new semantic level in which one can be absolutely precise...
    E. Dijkstra
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured