2

Possible Duplicate:
Generate UUID in Java

I need to generate a 9-digit unique code like the ones used on the back of products to identify them.

The codes should be non repeating and they should have no correlation between the numbers. And also the code should be all integers.

I need to generate them using java and simultaneously insert them into the database.

Community
  • 1
  • 1
haedes
  • 612
  • 2
  • 10
  • 23

4 Answers4

7

Generate a 9 digit random number and look it up against a database for uniqueness.

100000000 + random.nextInt(900000000)

or

String.format("%09d", random.nextInt(1000000000))
Praveen
  • 522
  • 1
  • 8
  • 17
2

Use Commons Lang's randomNumeric method:

http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/RandomStringUtils.html#randomNumeric(int)

Still, you'll have to check against your database for uniqueness.

Isaac
  • 16,458
  • 5
  • 57
  • 81
0
        int noToCreate = 1_000; // the number of numbers you need
        Set<Integer> randomNumbers = new HashSet<>(noToCreate);

        while (randomNumbers.size() < noToCreate) {
            // number is only added if it does not exist
            randomNumbers.add(ThreadLocalRandom.current().nextInt(100_000_000, 1_000_000_000));
        }
s106mo
  • 1,243
  • 2
  • 14
  • 20
  • Note that the underscore in the number format is a feature present since Java 1.7. Older source levels won't work. – Maarten Bodewes Sep 30 '12 at 17:59
  • -1 to not use a secure random function – Maarten Bodewes Sep 30 '12 at 18:05
  • @owlstead the requirement was not to use a secure random function, so I didn't use it, I don't see a reason for -1 – s106mo Oct 01 '12 at 15:56
  • I apologize if this is the case. If you could point out that requirement I would be happy to vote otherwise (mod you up and the question down) – Maarten Bodewes Oct 01 '12 at 16:52
  • 1
    I'm not sure if I get you right, but the requirement of Prakash was "The codes should be non repeating and they should have no correlation between the numbers." - I don't see why using ThreadLocalRandom should be not sufficient in this case... – s106mo Oct 04 '12 at 18:38
-1

I know doing this is bit odd, but still i think u can have you unique 9 digits number with almost no relation to each other.....

Ask u asked for there should have no correlation between the numbers

public class NumberGen {

    public static void main(String[] args) {

        long timeSeed = System.nanoTime(); // to get the current date time value

        double randSeed = Math.random() * 1000; // random number generation

        long midSeed = (long) (timeSeed * randSeed); // mixing up the time and
                                                        // rand number.

                                                        // variable timeSeed
                                                        // will be unique


                                                       // variable rand will 
                                                       // ensure no relation 
                                                      // between the numbers

        String s = midSeed + "";
        String subStr = s.substring(0, 9);

        int finalSeed = Integer.parseInt(subStr);    // integer value

        System.out.println(finalSeed);
    }

}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75