Thread: preventing same random numbers in JAVA

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    23

    preventing same random numbers in JAVA

    This is actually something I need help with in a java assignment. But I am pretty sure the concept is similar to C++. In other words if you know C++ you can probably help me with this minor set back.

    I basically want to generate random numbers (from 0 to 9). However I do not want it to generate duplicate random numbers. I have been messing around with this for a few hours but still can not come up with something to prevent the recurring of the same numbers. for example I do not want this......8, 8, 3, 5, 6. Where 8 is duplicated. I do not want that. Below is a snippet of part of my assignment that I have been messing around with trying to prevent this problem. So far I am stumped.




    Code:
    	for (int i = 0  ; i < lotteryNumbers.length; i++)
    	    {
    	       lotteryNumbers[i] = rN.nextInt(9);
    	       for(int x = 1; x < lotteryPicks.length; x++)
    	       {
    	           if (lotteryNumbers[i] == lotteryNumbers[x])
    	           {
    	              
    	              lotteryNumbers[x] = rN.nextInt(9);
    	              
    	                
    	           }
    	       }
    	       
    	    }



    Thank you

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Well in C++ you can use an STL (Standard Templated Library) container and randomize it.

    Example:
    Code:
    std::vector<int> myVec;
    for(int i = 0; i < 10; i++)
    {
        myVec.push_back(i);
    }
    std::random_shuffle(myVec);
    This is straight out of my head so I'm not totally sure if this code is 100&#37; right (especially for random_shuffle which I don't remember the parameters) but this should guide you.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    23
    Uhh I am not too familiar with that much c++ or at least all the STL Standard Templated Libraries.


    This is java but I feel I can get help for this particular problem I am having despite it being a C++ forum.

    Basically I am filling an array with random values from 0-9. But I do not want to end up with duplicate values in. For example I do not want to end up with duplicate 0's, 1's, 7's, etc. How would I go about to prevent that?

    Thank you
    Last edited by Rumproast23; 04-01-2007 at 08:49 PM.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Had you at least read his post and the FAQ you would know that the FAQ only explains how to generate random numbers without checking for duplicates.
    Last edited by Desolation; 04-01-2007 at 08:56 PM. Reason: Wrote 'fact' instead of 'FAQ'.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    23
    Quote Originally Posted by Desolation View Post
    Had you at least read his post and the FAQ you would know that the fact only explains how to generate random numbers without checking for duplicates.
    Exactly. I appreciate the help but that link did not solve the problem.
    Last edited by Rumproast23; 04-01-2007 at 08:55 PM.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    Oh, I think I have come up with an idea that might work on Java. If there are variable-sized containers in Java you can have a container hold {0, 1, 2 ... 9} and another empty container. Generate a random number which will be between 0 and "length" and then use that number as the index of the first container.

    Example:
    Code:
    void SetVector(std::vector<int>& myVec)
    {
        for(int i = 0; i < 10; i++)
        {
            myVec.push_back(i);
        }
    }
    
    // ...
    
    std::vector<int> myVec;
    do
    {
        // generate random number from 0 to myVec.size() - 1
        int random = rand() &#37; myVec.size() - 1;
        std::cout << myVec[random];
        // delete myVec[random] here, can't recall the correct function
    } while(myVec.size() > 0);
    Edit: I'm off to bed. No more posts for me today =P
    Last edited by Desolation; 04-01-2007 at 08:58 PM.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    From the original post it is unclear whether you want each random number to simply be different from just the one preceeding it, or from all that have preceeded it.

    For the former, jsut remember the last value and next time, loop until the result is different.

    For the later, you have two choices, keep a set of values that have not been used already, or keep a set of values that have been used already, then make sure it is or isn't (respectively) in the set. Set can entail a list, array, binary tree, hash table, or other container.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Using random_shuffle is the best way to go, and it doesn't take much knowledge of the STL (you can even use it on a C style array).

    If you cannot use random_shuffle, then just use the algorithm it uses. That algorithm is small but easy to get slightly off.

    Searching for random_shuffle on this site should give an example of its implementation, I know I've posted in a few threads that have done this.

    >> jsut remember the last value and next time, loop until the result is different.
    I don't like this solution in general because it is needlessly inefficient. For 10 items it is less of a big deal, but still the random_shuffle algorithm isn't that hard.
    Last edited by Daved; 04-01-2007 at 10:51 PM.

  9. #9
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    You seed it in much the same way you seed in C++ for random numbers. Java has a random number implementation. http://java.sun.com/j2se/1.5.0/docs/...il/Random.html

  10. #10
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by Rumproast23 View Post
    Uhh I am not too familiar with that much c++ or at least all the STL Standard Templated Libraries.


    This is java but I feel I can get help for this particular problem I am having despite it being a C++ forum.

    Basically I am filling an array with random values from 0-9. But I do not want to end up with duplicate values in. For example I do not want to end up with duplicate 0's, 1's, 7's, etc. How would I go about to prevent that?

    Thank you
    1. Check after you generated a number if it's already in the array, if yes, generate a new one.
    2. If you generate from 0-9 only, make sure your array is not bigger than 10, or else you got yourself an infinite loop.

    This is of course a horrible solution to the problem, because with an array of size 10, the chance for the last number to be different from the other numbers is 1/10 and your program might just loop unnecessarily before finding the last number.

  11. #11
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    This is of course a horrible solution to the problem, because with an array of size 10, the chance for the last number to be different from the other numbers is 1/10 and your program might just loop unnecessarily before finding the last number.
    Well, then, I'd say a better approach would be to start by creating a 10 element array and populating it with numbers 0-9. Then you can iterate through the array, choose a random number, say n, and swap the current element with the nth element. This way you get a random assorted array, no duplicates, and you don't redundantly check for used numbers. Plus it's incredibly simple to implement.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 12:59 PM
  5. How Cool is Java
    By dukemarlon in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 11-28-2002, 05:24 PM