Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: generate unique 9 digit number bug

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    2
    Thanked 1 Time in 1 Post

    Question generate unique 9 digit number bug

    Okay so I wrote a method that generates a random 9 digit number with each digit being unique, the problem i am having, is for some reason, there is a case where the method accidentally returns an 8 digit value instead of 9. This happens only a small percentage of the time. If you run the method a few 100 times you can see it. I dont know why it wont do what I want. Any tips or hints would be appreciated.

    The link to the java file is here:

    https://docs.google.com/open?id=0B7y...FVQMURCLTFCdTQ

    I already have a main method in there that creates 10000 or so random 9 digit numbers and prints each one along with the length. The method for
    generating the number is called randomGenerator.

     
     
    import java.util.ArrayList;
    import java.util.Random;
     
    public class Main {
     
        public static void main(String[] args)
        {
            for (int i = 0; i < 10000; i++) 
            {
                int n = generateRandom();
                System.out.println(n + " " + Integer.toString(n).length());
            }
        }
     
        public static int generateRandom()
        {
            Random generator = new Random();
            String sb = "";
            while (true)
            {
                String temp = Integer.toString(generator.nextInt(9));
                if (!sb.contains(temp)) 
                {
                    sb += temp;               
                    int n = temp.length();
                }
                if (sb.length() == 9) 
                    {
                       return Integer.parseInt(sb);
                    }
            }
        }
        public static void matchPin(int pin) {
            for (int i = 0; i < 362880; i++) {
                int n = generateRandom();
                if (!list.contains(n)) {
                    System.out.println(n + " - Iteration: " + i);
                    if (pin == n) {
                        System.out.println("Pin: " + pin);
                        System.out.println("N :" + n);
                        return;
                    } else {
                        list.add(n);
                    }
                }
     
            }
            System.out.println("Exited");
        }
        public static ArrayList<Integer> list = new ArrayList<Integer>();
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: generate unique 9 digit number bug

    You may have to post the pertinent code here in the forum. Many, myself included, will not click on links. Also, since you're asking for free help from volunteers, it's not asking too much for you to try to make this help as easy as possible to give.

    Please click on the forum FAQ to see how to do this and how to use code tags so that your code retains its formatting and is readable.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: generate unique 9 digit number bug

    This happens only a small percentage of the time.
    Leading zeros? But I agree with curmudgeon: post the code.

  4. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: generate unique 9 digit number bug

    Sorry for taking so long i posted the code on the original message.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: generate unique 9 digit number bug

    Can you post some examples of the 8 digit numbers that were generated?

    How does the code handle leading 0s?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: generate unique 9 digit number bug

    Well I dont see any with zeros in the beggining, does that mean java ignores leading zeros and deletes them?

    540326781 9
    457230168 9
    51328764 8
    250871643 9
    370418652 9
    206357814 9
    157204638 9
    783261054 9
    716524038 9
    576014328 9
    842365710 9
    754163028 9
    324610758 9
    601725348 9
    728056134 9
    452630187 9
    413568702 9
    328416507 9
    725810643 9
    328061754 9
    560438712 9
    130784652 9
    820645713 9
    716038245 9
    386174250 9
    850674312 9
    376128540 9
    640357812 9
    162437058 9
    210687543 9
    628435710 9
    157680432 9
    803574216 9
    84716235 8
    204738651 9
    308615247 9
    671058234 9
    208154763 9
    418075263 9
    327651084 9
    81245376 8
    632074518 9
    236081574 9
    685371402 9
    185473026 9
    823671045 9
    325674810 9
    803617542 9
    57214368 8
    218654037 9
    563810742 9
    824071536 9
    857431602 9
    234158670 9
    410325687 9
    206417835 9
    314852076 9
    217658034 9
    17286435 8
    214570863 9
    378654102 9
    618057234 9
    120638754 9
    35267814 8
    285374016 9
    58417362 8
    380145672 9

  7. #7
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: generate unique 9 digit number bug

    Look at your code, you tell it to create a random number between 0 and 9. In the Random class however, the generated number is between 0 and n inclusive meaning the any number between 0 - (n-1) is generated. Look at all the generated numbers above, they may have nine digits, but none of them have a a nine in the digits.

    Also, the small percentage that are 8 digits are going to happen unless you add a check. Let's do some math:

    probability of first value equaling ten:
     
    1 value looked for
    9 possible
     
    probability is 1/9 or 11.1(...)% of the time. 
     
    *(...) means repeating
     
    first two values are zero:
     
    (1/9)*(1/9) or (1/9)^2
     
    1/81 or 1.23456780(...)% of the time. 
     
    all 9 equal zero:
     
    (1/9)^9
     
    1/387,420,489 or (2.5811747917131971819900315081167532159095488622957161153... × 10^-7) percent.

    So basically, if you run the algorithm 387,420,489 times, you will probably have this number "000000000" be result. But since you generate numbers without a repeating value, you will have an 8 digit number (leading zero) about 11% of the time. If you don't want zeros in your generated values, you can do this, which creates a value between one and nine:

    Random random = new Random();
     
    int val =random.nextInt(9) + 1;

    Or

    If you want to avoid the first value being zero, when you generate the first number, there are a few ways to do it.

    Random rand = new Random();
     
    //method 1
     
    int val = 0;
    do{
         val = rand.nextInt(10);
    }while(val!=0);
     
    //method 2
     
    val = rand.nextInt(9)+1;

  8. The Following User Says Thank You to aesguitar For This Useful Post:

    matinm90 (December 22nd, 2012)

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: generate unique 9 digit number bug

    What does the code do with the 9 digit number? Does it have to be an int value?
    What if the code was changed in this way:
        public static String generateRandom()
          ....
                       return sb.toString();
    Then it will always return 9 digits.
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    matinm90 (December 22nd, 2012)

  11. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: generate unique 9 digit number bug

    Oh so then if I took that string and converted it back into a Integer, would it retain the whole value? or would some numbers get lost? hope that made sense.

  12. #10
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: generate unique 9 digit number bug

    Why do you even have to convert it to a integer? Why not just keep it a String?

  13. #11
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: generate unique 9 digit number bug

    Quote Originally Posted by aesguitar View Post
    Look at your code, you tell it to create a random number between 0 and 9. In the Random class however, the generated number is between 0 and n inclusive meaning the any number between 0 - (n-1) is generated. Look at all the generated numbers above, they may have nine digits, but none of them have a a nine in the digits.

    Also, the small percentage that are 8 digits are going to happen unless you add a check. Let's do some math:

    probability of first value equaling ten:
     
    1 value looked for
    9 possible
     
    probability is 1/9 or 11.1(...)% of the time. 
     
    *(...) means repeating
     
    first two values are zero:
     
    (1/9)*(1/9) or (1/9)^2
     
    1/81 or 1.23456780(...)% of the time. 
     
    all 9 equal zero:
     
    (1/9)^9
     
    1/387,420,489 or (2.5811747917131971819900315081167532159095488622957161153... × 10^-7) percent.

    So basically, if you run the algorithm 387,420,489 times, you will probably have this number "000000000" be result. But since you generate numbers without a repeating value, you will have an 8 digit number (leading zero) about 11% of the time. If you don't want zeros in your generated values, you can do this, which creates a value between one and nine:

    Random random = new Random();
     
    int val =random.nextInt(9) + 1;

    Or

    If you want to avoid the first value being zero, when you generate the first number, there are a few ways to do it.

    Random rand = new Random();
     
    //method 1
     
    int val = 0;
    do{
         val = rand.nextInt(10);
    }while(val!=0);
     
    //method 2
     
    val = rand.nextInt(9)+1;
    Oh okay that makes sense thank you, I didnt realize that the random class did that. I thought nextInt(9) created number from 0 to 9, but i guess not. Thank you!

  14. #12
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: generate unique 9 digit number bug

    Just make sure your first value isn't zero if you want to keep it as an Integer.

  15. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: generate unique 9 digit number bug

    converted it back into a Integer
    What would "00000001" convert to?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. generate a random number from 100 to 150, inclusive in applet
    By chonch in forum Java Theory & Questions
    Replies: 12
    Last Post: February 14th, 2014, 05:44 AM
  2. Replies: 2
    Last Post: January 6th, 2013, 04:20 AM
  3. Generate random number in Java
    By waiheng1986 in forum Java Programming Tutorials
    Replies: 1
    Last Post: March 11th, 2012, 12:55 PM
  4. Replies: 5
    Last Post: November 26th, 2011, 03:08 PM
  5. How to generate 13 digit numbers to text file
    By duanedtp in forum Java Theory & Questions
    Replies: 3
    Last Post: April 27th, 2011, 11:30 AM